Reputation: 57
First of all, let me show you my classes:
public class Model
{
public Model Parent {get; set;}
public ObservableCollection<Model> Childs {get; set;}
public ObservableCollection<Partner> Partners {get; set;}
public bool IsTopElement => Parent == null;
}
public class Partner
{
public bool IsActive {get; set;}
public string PartnerRole {get; set;}
public bool IsCustomer => PartnerRole == "Customer"
}
As you can see, Model
is a hierarchy as it can have a parent and a list of childs. I have a UserControl whose DataContext is set to an instance of Model
. In this UserControl I have a DataGrid whose ItemsSource is bound to Partners
. In this DataGrid I have a DataGridCheckBoxColumn that is bound to IsActive
property of Partner
object and several DataGridTextColumns.
What I want to achieve is that if the current Model
object is NOT the top element (has no parent), all IsActive
-Checkbox cells that belong to a Partner
entry whose IsCustomer == True
shall be disabled.
I tried the following XAML code:
<UserControl DataContext="{Binding Model}">
<DataGrid ItemsSource="{Binding Partners}"
AutoGenerateColumns="False"
CanUserResizeRows="False"
CanUserAddRows="False"
CanUserDeleteRows="False"
RowDetailsVisibilityMode="VisibleWhenSelected">
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding IsActive}">
<DataGridCheckBoxColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Model.IsTopElement}" Value="False"/>
<Condition Binding="{Binding ???}"/> <!-- I'd have expected Binding="{Binding IsCustomer}" Value="True" to work, but it doesn't-->
</MultiDataTrigger.Conditions>
<Setter Property="IsEnabled" Value="False"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</DataGridCheckBoxColumn.CellStyle>
</DataGridCheckBoxColumn>
</DataGrid.Columns>
</DataGrid>
</UserControl>
The problem I'm facing now is that I don't have access to the property IsCustomer
of the Partner
object in the DataGrid in the second Condition of the MultiDataTrigger. So how can I access this property there? Also, I didn't expect to have access to IsTopElement
property of Model
in the first Condition because the ItemsSource of the DataGrid is bound to Partners
but apparently I have, which is rather confusing to me.
Any help would be much appreciated. Please let me know if you need further information.
Upvotes: 1
Views: 373
Reputation: 169350
The DataContext
of the DataGridCell
is a Partner
object. You could use a {RelativeSource}
to bind to the parent UserControl
's DataContext
. Try this:
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding DataContext.IsTopElement, RelativeSource={RelativeSource AncestorType=UserControl}}" Value="False"/>
<Condition Binding="{Binding IsCustomer}" Value="True"/>
</MultiDataTrigger.Conditions>
<Setter Property="IsEnabled" Value="False"/>
</MultiDataTrigger>
</Style.Triggers>
</Style>
Upvotes: 1