Search code examples
wpfdata-bindingcode-behind

How can I get the UpdateSourceTrigger settings from code for a DataGridColumn binding?


I have a DataGrid control that has a TextColumn

    <DataGrid ItemsSource="{Binding Path=Dvm.Data}"
              Name="GrdName"
              AutoGenerateColumns="False"
              Margin="5"
              SelectionMode="Single">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Column 1"
                                Binding="{Binding Path=Col1, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
                                Width="*"/>
        </DataGrid.Columns>
    </DataGrid>

I have a custom control that requires me to test what the UpdateSourceTrigger setting is, but I am having trouble accessing that from code. I would like to do something like this:

BindingOperations.GetBindingExpression(GrdName.Columns[0], DataGridTextColumn.BindingProperty);

But, BindingProperty is not actually a dependency property, so I can't do that. Does anyone know how I would go about getting the binding expression for the Binding property for that column so I can get the UpdateSourceTrigger setting?

Thanks, Matt


Solution

  • The Binding property is not a DependancyProperty so you will have to access using normal public properties. So you will have to do a bit of casting as the Binding property in DataGridTextColumn is type BindingBase you will have to cast as Binding to access UpdateSourceTrigger. And since DataGridTextColumn is derived from DataGridBoundColumn you can cast from that to make it a bit more generic

    Something like this should work:

       var columnUpdateSourceTrigger = ((GrdName.Columns[0] as DataGridBoundColumn).Binding as Binding).UpdateSourceTrigger;