0
votes

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?

1
"It doesn't compile and complains on the binding"; can you provide the actual error details? - RJ Lohan
Thank you, that was good point. The error message was that it cannot find IAccount. - mlemanczyk
I got the binding working. But this is just part of the question. I still don't know how to apply the style and resolve DataTemplate.Triggers in the first grid. This is my first question on StackOverflow. Should I post the binding as answer and open separate questions for styling/first grid? Or rather update the question with new discoveries? Should first edit with tries be removed then? - mlemanczyk

1 Answers

0
votes

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.