I'd like to manipulate a control on the form, if the value of Closed
property is true
.
There is a similar question describing DataTemplate
triggers. I believe this is what I need, but I cannot get it working.
My DataGrid
is defined as follows:
<DataGrid SelectedItem="{Binding SelectedAccount, Mode=TwoWay}" ItemsSource="{Binding Accounts, Mode=TwoWay}">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Account Name">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox x:Name="AccountName" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" />
<DataTemplate.Triggers>
<DataTrigger Binding="{RelativeSource FindAncestor, AncestorType={x:Type IAccount}}" Value="True">
... property adjustments ...
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
...
It doesn't compile and raises the error
Unable to cast object of type 'System.Windows.Data.RelativeSource' to type 'System.Windows.Data.BindingBase'
I've also tried applying the binding as described in the other question.
<DataTemplate DataType="models:IAccount"> OR <DataTemplate DataType="models:Account">
...
<DataTemplate.Triggers>
<DataTrigger Binding="{Closed}" Value="True">
... property adjustments ...
</DataTrigger>
</DataTemplate.Triggers>
But that raised other error messages
Error 1 The tag 'Closed' does not exist in XML namespace 'http ://schemas.microsoft.com/winfx/2006/xaml/presentation'.
Error 2 The type 'Closed' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.
Error 3 Closed is not supported in a Windows Presentation Foundation (WPF) project.
I am very new to WPF and bindings. Would you please help?
The solution turned out to be very simple. The binding needs to be defined as follows.
<DataTemplate DataType="models:IAccount"> OR <DataTemplate DataType="models:Account">
...
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Closed}" Value="True">
... property adjustments ...
</DataTrigger>
</DataTemplate.Triggers>
Please remember to add a namespace to your types in xaml, too.
Thank you everyone for correcting my first question and your efforts to help.