Reputation: 309
suppose i have a datagrid in which there is 2 columns and 1 expander in each column.
<DataGrid.Columns>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Expander Style="{StaticResource PlusMinusExpanderStyle}"
ToolTip="Rule Details"
Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},Mode=FindAncestor},Path=DataContext.IsRuleDetailsExpanderVisible, Converter={StaticResource BooleanToVisibilityConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
>
<Expander.IsExpanded>
<Binding RelativeSource="{RelativeSource AncestorType={x:Type DataGridRow}}"
Path="DetailsVisibility"
Mode="TwoWay">
<Binding.Converter>
<converters:BooleanToVisibilityDataGridRow FalseToVisibility="Collapsed" />
</Binding.Converter>
</Binding>
</Expander.IsExpanded>
</Expander>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Status">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid>
<Ellipse Grid.Column="1"
Width="15"
Height="15"
Fill="{Binding Status,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged, Converter={StaticResource ColorToBrushConverter}}" />
</Grid>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Add Rule">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Width="22" Height="22"
ToolTip="Add Rule"
BorderThickness="0"
Background="Transparent"
Style="{StaticResource TransparentStyle}"
IsEnabled="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},Mode=FindAncestor},Path=DataContext.IsMonitorDataGridColumnEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl},Mode=FindAncestor},Path=DataContext.AddRuleCommand}"
CommandParameter="{Binding ElementName=MonitorDataGrid, Path=SelectedItem}">
<Image Source="/OBDApplication;component/Images/addRule1.png"
Width="17"
Height="17"
ToolTip="Add Rule" />
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
Now i have to control the visibility of each expander. on click of Add rule button, a new window appears which takes some input and add data to a collection. if there is any data present in a rule then only i should show the expander icon.
so, i have bound a boolean property to control visibility and after a rule is added to collection, i am making that boolean property as true. But the problem is that expander is visible for every row cause boolean returns true even if there is a record in the collection.
so how can i control the visiblity of expander for each row?
Upvotes: 0
Views: 747
Reputation: 309
I have fixed it with my own.
create a boolean property in the model itself so it will check for each item. And then bind the expander visibility with that model's property.
My updated xaml code for expander looks like this:
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Expander Style="{StaticResource PlusMinusExpanderStyle}"
ToolTip="Rule Details"
Visibility="{Binding ISMonitorExanderVisible, Converter={StaticResource BooleanToVisibilityConverter}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
>
<Expander.IsExpanded>
<Binding RelativeSource="{RelativeSource AncestorType={x:Type DataGridRow}}"
Path="DetailsVisibility"
Mode="TwoWay">
<Binding.Converter>
<converters:BooleanToVisibilityDataGridRow FalseToVisibility="Collapsed" />
</Binding.Converter>
</Binding>
</Expander.IsExpanded>
</Expander>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
I am still looking for a different approach.
Upvotes: 1