0

stackoverflow!

Starting with my code:

XAML

<DataGrid Margin="25,112,25,10" Name="datGrid" RowHeight="30"
        ColumnWidth="150" BorderThickness="1" 
        Style="{StaticResource AzureDataGrid}" IsReadOnly="False"
        AutoGenerateColumns="False">
       <DataGrid.Columns>
           <DataGridTemplateColumn>
               <DataGridTemplateColumn.HeaderTemplate>
                   <DataTemplate>
                       <CheckBox Content="CREATE" Name="headerCheckBox" 
                           FontWeight="Bold" Width="Auto" 
                           Checked="headerCheckBox_Checked"/>
                   </DataTemplate>
               </DataGridTemplateColumn.HeaderTemplate>
               <DataGridTemplateColumn.CellTemplate>
                   <DataTemplate>
                       <CheckBox IsChecked="{Binding Path=IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
                   </DataTemplate>
               </DataGridTemplateColumn.CellTemplate>
               <DataGridTemplateColumn.CellStyle>
                   <Style TargetType="DataGridCell">
                       <Setter Property="HorizontalAlignment"  Value="Center"/>
                   </Style>
               </DataGridTemplateColumn.CellStyle>
           </DataGridTemplateColumn>
       <DataGridTextColumn Header="Username" Binding="{Binding Path=Username}"/>
       <DataGridTextColumn Header="First Name" Binding="{Binding Path=FirstName}"/>
       <DataGridTextColumn Header="Last Name" Binding="{Binding Path=LastName}"/>
       <DataGridTextColumn Header="Email" Binding="{Binding Path=Email}"/>
   </DataGrid.Columns>

</DataGrid>

And C#

public partial class MainWindow : Window
{
    ObservableCollection<MyData> MyItems = new ObservableCollection<MyData>();

    public MainWindow()
    {
        datGrid.ItemsSource = MyItems;
        MyItems.Add(new MyData { IsChecked = false, Username = "apetecca", FirstName = "Anthony", LastName = "Petecca", Email = "[email protected]"});
        MyItems.Add(new MyData { IsChecked = true, Username = "jhalls", FirstName = "Jake", LastName = "Halls", Email = "[email protected]" });

    }

    public class MyData
    {
        public bool IsChecked { get; set; }
        public string Username { get; set; } 
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
    }

    private void headerCheckBox_Checked(object sender, RoutedEventArgs e)
    {
        foreach (var item in MyItems)
        {
            item.IsChecked = true;
        }
    }

When I click on the headerCheckBox to check it, I've watched the variables in the foreach loop to see it is changing the items from false to true but doesn't visually show that on the DataGrid. However, if I manually check or uncheck the boxes, it displays correctly when going through that loop. The loop correctly sets the values but does not change the GUI display.

Everything I have seen online indicates it has to do with TwoWay mode and UpdateSourceTrigger. Both of these are set and I can't seem to find anything else on these.

Any help would be greatly appreciated!

EDIT-

I also tried changing my class to look like this

public class MyData
{
    private bool _isChecked;
    public bool IsChecked
    {
        get { return _isChecked; }
        set 
        { 
            _isChecked = value;
            OnPropertyChanged("IsChecked");
        }
    }
    public string Username { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }

    private void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

This didn't resolve it either.

3
  • How about implementing INotifyPropertyChanged in MyData class? This is pretty basic thing that every WPF-er should know. Commented Nov 16, 2015 at 19:17
  • Wow. That's all I had to do was add : INotifyPropertyChanged to MyData. I will have to read about this as I'm still new to WPF and C# in general. Thanks!!! :] Commented Nov 16, 2015 at 19:22
  • Well, you need to do all that boring thing with all your properties (otherwise you can have similar problems), but you got the point :-) Commented Nov 16, 2015 at 19:27

1 Answer 1

3

Implement INotifyPropertyChanged and then have your properties raise event to make this happen.

Something like:

public class MyData : INotifyPropertyChanged
{
    private bool isChecked;
    public bool IsChecked 
    { 
        get { return isChecked; } 
        set
        {
           if (isChecked != value)
           {
              isChecked = value;
              OnPropertyChanged("IsChecked");
           }
    }
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.