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? 🙂
TableProperty class has to implement INotifyPropertyChanged and raise it properly.