2

I have already read some posts, but no one helped me with my problem. So, i have a View with a Viewmodel and inside the View a DataGrid boudn to a ObservableCollection inside the viewmodel. The Selected Item is also Bound to Type T == ObservableCollection

public ObservableCollection<TableProperty> TableProperties
    {
        get
        {
            return tableProperties;
        }
        set
        {
            if (tableProperties != value)
            {
                tableProperties = value;
                OnPropertyChanged("TableProperties");
            }
        }
    }

public TableProperty Property
    {
        get
        {
            return property;
        }
        set
        {
            property = value;
            OnPropertyChanged("Property");
        }
    }

And here the DataGrid:

<toolkit:DataGrid AutoGenerateColumns="False" 
                      UseLayoutRounding="True" 
                      ItemsSource="{Binding Path=TableProperties,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,NotifyOnTargetUpdated=True}"
                      SelectedItem="{Binding Path=Property,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" 
                      [...]>
        <toolkit:DataGrid.Columns>

Now i want to implement a logic that changing a checkbox triggers a command setting some values of the selected item:

<toolkit:DataGridTemplateColumn Header="Mandatory" IsReadOnly="False">
                <toolkit:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                            <CheckBox IsChecked="{Binding Path=Mandatory,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
                                <CheckBox.Style>
                                    <Style TargetType="{x:Type CheckBox}">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Path=MandatoryDB}" Value="True">
                                                <Setter Property="IsEnabled" Value="False" />
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding Path=MandatoryDB}" Value="False">
                                                <Setter Property="IsEnabled" Value="True" />
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </CheckBox.Style>
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Checked">
                                        <cmd:EventToCommand Command="{Binding Path=DataContext.SetMandatory, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </CheckBox>
                        </StackPanel>
                    </DataTemplate>
                </toolkit:DataGridTemplateColumn.CellTemplate>
            </toolkit:DataGridTemplateColumn>

And this is the Command behind it:

 private void OnSetMandatory()
    {
        property.Visible = true;
        property.ReadOnly = false;
        property.VisibleInGrid = true;
        property.UIPropertyName = DateTime.Now.TimeOfDay.ToString();
        OnPropertyChanged("Property");
        OnPropertyChanged("TableProperties");
    }

The Problem is: when I change the properties the item inside the collection has also been updated and it goes properly inside the Property Getter... If I call Datagrid.Items.Refresh() inside the view directly it will also display the values correctly, but not automatically from updating the collection.

So do you have any idea? :)

1 Answer 1

3

TableProperty class has to implement INotifyPropertyChanged and raise it properly.

3
  • Ah I got it already implemented correctly. Thank you so much dude! :) Jun 20, 2012 at 8:16
  • @JohannesWanzek It appears that a portion of the comments's been removed so I can only see your satisfaction but not the actual proper implementation... Feb 4, 2015 at 9:47
  • 1
    Hello Konrad, just implement the INotifyPropertyChanged Interface to your class. stackoverflow.com/questions/1315621/… If you have Resharper it would do it for you! Feb 5, 2015 at 14:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.