WPF DataGridCell Binding Problem
-
I have a DataGrid bound to a list of models. On the ViewModel is a property called AreFieldsEnabled. When the user clicks Edit the it sets AreFieldsEnabled to true and the datagrid becomes enabled/ I have this style
<Style x:Key="dataGridCurrencyCellStyle"
BasedOn="{StaticResource dataGridCellStyle}"
TargetType="{x:Type DataGridCell}"><Setter Property="Width" Value="75"/> <Setter Property="TextBlock.TextAlignment" Value="Left"/> <Setter Property="TextBlock.Text" Value="{Binding StringFormat=C}"/> <Setter Property="IsEnabled" Value="{Binding DataContext.AreFieldsEnabled, ElementName=budgetControl}"/>
</Style>
What I need do now is a change it to a MultiTrigger. On the model is a bool property called IsCellEnabled which I set programmatically when loading the data.
<Style x:Key="dataGridCurrencyCellStyle"
BasedOn="{StaticResource dataGridCellStyle}"
TargetType="{x:Type DataGridCell}"><Setter Property="Width" Value="75"/> <Setter Property="TextBlock.TextAlignment" Value="Left"/> <Setter Property="TextBlock.Text" Value="{Binding StringFormat=C}"/> <Setter Property="IsEnabled" Value="False"/> <Style.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="AreFieldsEnabled" Value="True" /> <Condition Property="IsCellEnabled " Value="True" /> </MultiTrigger.Conditions> <MultiTrigger.Setters> <Setter Property="IsEditing" Value="True" /> </MultiTrigger.Setters> </MultiTrigger> </Style.Triggers>
</Style>
Now, both conditions are wrong and won't compile. So, the AreFieldsEnabled property is on the ViewModel. The IsCellEnabled property is on the Model bound to the grid. What's the right way to do this?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
I have a DataGrid bound to a list of models. On the ViewModel is a property called AreFieldsEnabled. When the user clicks Edit the it sets AreFieldsEnabled to true and the datagrid becomes enabled/ I have this style
<Style x:Key="dataGridCurrencyCellStyle"
BasedOn="{StaticResource dataGridCellStyle}"
TargetType="{x:Type DataGridCell}"><Setter Property="Width" Value="75"/> <Setter Property="TextBlock.TextAlignment" Value="Left"/> <Setter Property="TextBlock.Text" Value="{Binding StringFormat=C}"/> <Setter Property="IsEnabled" Value="{Binding DataContext.AreFieldsEnabled, ElementName=budgetControl}"/>
</Style>
What I need do now is a change it to a MultiTrigger. On the model is a bool property called IsCellEnabled which I set programmatically when loading the data.
<Style x:Key="dataGridCurrencyCellStyle"
BasedOn="{StaticResource dataGridCellStyle}"
TargetType="{x:Type DataGridCell}"><Setter Property="Width" Value="75"/> <Setter Property="TextBlock.TextAlignment" Value="Left"/> <Setter Property="TextBlock.Text" Value="{Binding StringFormat=C}"/> <Setter Property="IsEnabled" Value="False"/> <Style.Triggers> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="AreFieldsEnabled" Value="True" /> <Condition Property="IsCellEnabled " Value="True" /> </MultiTrigger.Conditions> <MultiTrigger.Setters> <Setter Property="IsEditing" Value="True" /> </MultiTrigger.Setters> </MultiTrigger> </Style.Triggers>
</Style>
Now, both conditions are wrong and won't compile. So, the AreFieldsEnabled property is on the ViewModel. The IsCellEnabled property is on the Model bound to the grid. What's the right way to do this?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Use a
MultiDataTrigger
when the conditions depend on a binding rather than the styled control's properties.<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding DataContext.AreFieldsEnabled, ElementName=budgetControl}" Value="True" />
<Condition Binding="{Binding IsCellEnabled}" Value="True" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="IsEditing" Value="True" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style>MultiDataTrigger Class (System.Windows) | Microsoft Docs[^] WPF MultiTrigger and MultiDataTrigger - The complete WPF tutorial[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Use a
MultiDataTrigger
when the conditions depend on a binding rather than the styled control's properties.<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding DataContext.AreFieldsEnabled, ElementName=budgetControl}" Value="True" />
<Condition Binding="{Binding IsCellEnabled}" Value="True" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="IsEditing" Value="True" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style>MultiDataTrigger Class (System.Windows) | Microsoft Docs[^] WPF MultiTrigger and MultiDataTrigger - The complete WPF tutorial[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks. That only partly fixed it
<Style x:Key="dataGridCurrencyCellStyle"
BasedOn="{StaticResource dataGridCellStyle}"
TargetType="{x:Type DataGridCell}"><Setter Property="Width" Value="75"/> <Setter Property="TextBlock.TextAlignment" Value="Left"/> <Setter Property="TextBlock.Text" Value="{Binding StringFormat=C}"/> <Setter Property="IsEnabled" Value="False"/> <Style.Triggers> <MultiDataTrigger> <MultiDataTrigger.Conditions> <Condition Binding="{Binding DataContext.AreFieldsEnabled, ElementName=budgetControl}" Value="True" /> <Condition Binding="{Binding IsCellEnabled}" Value="True" /> </MultiDataTrigger.Conditions> <MultiDataTrigger.Setters> <Setter Property="IsEnabled" Value="True" /> </MultiDataTrigger.Setters> </MultiDataTrigger> </Style.Triggers>
</Style>
The 2nd condition with the IsCellEnabled isn't finding the model the row is bound to. Not sure how to reference that from this style. I get a binding error "BindingExpression path error: 'IsCellEnabled' property not found on 'object' ''DataRowView'"
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
Use a
MultiDataTrigger
when the conditions depend on a binding rather than the styled control's properties.<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding DataContext.AreFieldsEnabled, ElementName=budgetControl}" Value="True" />
<Condition Binding="{Binding IsCellEnabled}" Value="True" />
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="IsEditing" Value="True" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style>MultiDataTrigger Class (System.Windows) | Microsoft Docs[^] WPF MultiTrigger and MultiDataTrigger - The complete WPF tutorial[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
I'm still stuck on this. I can't seem to get the second trigger condition to bind correctly. How would I bind the condition to the IsCellEnabled property on the model?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
-
I'm still stuck on this. I can't seem to get the second trigger condition to bind correctly. How would I bind the condition to the IsCellEnabled property on the model?
If it's not broken, fix it until it is. Everything makes sense in someone's mind. Ya can't fix stupid.
Kevin Marois wrote:
'IsCellEnabled' property not found on 'object' ''DataRowView'"
You're not binding to the model; you're binding to a
DataView
. You could try{Binding [IsCellEnabled]}
, assuming that is a column in your view.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer