vendredi 30 octobre 2015

C# WPF Binding CheckBox in GridData to underlying Array, data not changed

Very new to WPF. Having issues with Binding a CheckBox to the underlying Array data. The value gets set at start up with the data, but checking it does not result in the underlying data to be changed. Note that I have implemented the check box to allow multiple items to be selected. Also note that I have verified that it is not because of focus (checkbox having to lose focus) because I have clicked other stuff before hitting the "migrate" button which checks the underlying data. I am sure I am missing some autowire magic, but I have checked out a ton of other blogs with no success.

The check box not working is :

       <DataGridTemplateColumn Width="60" Header="Migrate">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding Checked}" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

The code is pretty simple, MainWindow.xaml:

<Window x:Class="FFFJsonMigrator.MainWindow"
        xmlns="http://ift.tt/o66D3f"
        xmlns:x="http://ift.tt/mPTqtT"
        xmlns:d="http://ift.tt/pHvyf2"
        xmlns:mc="http://ift.tt/pzd6Lm"
        xmlns:Code="clr-namespace:FFFJsonMigrator.Code"
        mc:Ignorable="d"
        Title="JSON Migrator" Height="482" Width="729">
    <Grid Background="#FF9DB2C1" Margin="0,0,2,-21">
        <Grid.DataContext>
            <Code:FFFJsonMigrator/>
        </Grid.DataContext>
        <DataGrid x:Name="dataGrid"  ItemsSource="{Binding myAccountList}" SelectionMode="Extended" HorizontalAlignment="Left" Margin="10,63,0,0" VerticalAlignment="Top" Height="352" Width="685" CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" HeadersVisibility="Column" AlternatingRowBackground="#FFE7F3FA" AlternationCount="1" CanUserAddRows="False" CanUserDeleteRows="False" CanUserResizeRows="False" ColumnWidth="Auto" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn Width="60" Header="Migrate">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox IsChecked="{Binding Checked}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Binding="{Binding Id}" IsReadOnly="True"  Header="Id" Width="40"/>
                <DataGridTextColumn Binding="{Binding AccountName}" IsReadOnly="True"  Header="Account Name" Width="250"/>
                <DataGridTextColumn Binding="{Binding Forms}"  IsReadOnly="True"  Header="Forms" Width="60"/>
                <DataGridTextColumn Binding="{Binding Pending}"  IsReadOnly="True"  Header="Pending"  Width="60"/>
                <DataGridTextColumn Binding="{Binding Archived}" IsReadOnly="True"   Header="Archived"  Width="60"/>
                <DataGridTextColumn Binding="{Binding Total}"  IsReadOnly="True"  Header="Total"  Width="60"/>
            </DataGrid.Columns>
        </DataGrid>
        <ComboBox x:Name="envComboBox" HorizontalAlignment="Left" Margin="10,32,0,0" VerticalAlignment="Top" Width="120"/>
        <Button x:Name="refreshButton" Content="Refresh" HorizontalAlignment="Left" Margin="144,32,0,0" VerticalAlignment="Top" Width="75"/>
        <CheckBox x:Name="activeOnlyCheckbox" Content="Active Accounts Only" HorizontalAlignment="Left" Margin="14,427,0,0" VerticalAlignment="Top"/>
        <Button x:Name="migrateButton" Command="{Binding MigrateCommand}" IsEnabled="True" Content="Migrate Forms" HorizontalAlignment="Left" Margin="506,424,0,0" VerticalAlignment="Top" Width="75"/>
        <TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="236,10,0,0" TextWrapping="Wrap" Text="Select the accounts to be migrated. The JSON in the Form, FormArchive and FormPending tables will be copied to Azure and the path to the file added to the JsonPath columns" VerticalAlignment="Top" Width="353"/>
    </Grid>
</Window>

The FFFJsonMigrator.cs :

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;

namespace FFFJsonMigrator.Code
{
    public class FFFJsonMigrator : INotifyPropertyChanged
    {
        public ObservableCollection<AccountData> myAccountList { get; set; }

        public FFFJsonMigrator()
        {
            myAccountList = new ObservableCollection<AccountData>();
            myAccountList.Add(new AccountData(false, "Test1", 1232344, 2, 3, 4, 9));
            myAccountList.Add(new AccountData(false, "Test2", 2222345, 1, 1, 1, 3)); ;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        private void MigrateMessage()
        {
            if (IsAnyToMigrate())
            {
                // Configure the message box to be displayed
                string messageBoxText = "Qre you sure you want to Migrate the selected Account JSON files?";
                string caption = "Migrate Json Files for Selected Accounts";
                MessageBoxButton button = MessageBoxButton.YesNo;
                MessageBoxImage icon = MessageBoxImage.Warning;

                // Display message box
                MessageBoxResult result = MessageBox.Show(messageBoxText, caption, button, icon);

                if (result == MessageBoxResult.Yes)
                {
                    ExecuteMigrate();
                }
            }
            else
            {
                // Configure the message box to be displayed
                string messageBoxText = "You have not chosen any accounts to migrate. Please select one or more Accounts to migrate.";
                string caption = "No Accounts To Migrate";
                MessageBoxButton button = MessageBoxButton.OK;
                MessageBoxImage icon = MessageBoxImage.Warning;

                // Display message box
                MessageBox.Show(messageBoxText, caption, button, icon);
            }

        }

        public bool IsAnyToMigrate()
        {
            foreach (AccountData accnt in myAccountList)
            {
                if (accnt.Checked)
                    return true;

            }
            return false;
        }

        private ICommand _migrateCommand;
        public ICommand MigrateCommand
        {
            get
            {
                return _migrateCommand ?? (_migrateCommand = new CommandHandler(() => MigrateMessage(), true));
            }
        }

        public class CommandHandler : ICommand
        {
            private Action _action;
            private bool _canExecute;
            public CommandHandler(Action action, bool canExecute)
            {
                _action = action;
                _canExecute = canExecute;
            }

            public bool CanExecute(object parameter)
            {
                return _canExecute;
            }

            public event EventHandler CanExecuteChanged;

            public void Execute(object parameter)
            {
                _action();
            }
        }
    }

    public class AccountData
    {
        public AccountData(bool check, string accountName, int id, int forms, int pending, int archived, int total)
        {
            AccountName = accountName;
            Id = id;
            Forms = forms;
            Pending = pending;
            Archived = archived;
            Total = total;
            Checked = check;
        }

        public bool Checked { get; set; }
        public string AccountName { get; set; }
        public int Id { get; set; }
        public int Forms { get; set; }
        public int Pending { get; set; }
        public int Archived { get; set; }
        public int Total { get; set; }
    }
}

And finally, the actual window itself. Note I am doing the check then the "Migrate" button is hit...

enter image description here




Aucun commentaire:

Enregistrer un commentaire