【发布时间】:2019-06-19 12:43:50
【问题描述】:
我正在使用DataGrid,并且在单击按钮时,我希望能够在DataGrid 列的CellTemplate 和EditingCellTemplate 之间进行切换。
加载时,DataGrid 显示具有权限级别的CellTemplate。
当用户在权限级别单元格内双击时,模板变为EditingCellTemplate 并出现ItemsControl 按钮。
当用户按下管理员、读取或写入这些按钮之一时,我希望权限级别模板显示 CellTemplate 仅显示文本而不是 EditingCellTemplate。
我考虑过使用一种行为,但不确定它是如何工作的。我的两个 CellTemplate 都在资源字典中。
显示文本的CellTemplate
<DataTemplate x:Key="PermissionTemplate">
<Border>
<Label Content="{Binding Path=PermissionLevel.Access}" />
</Border>
</DataTemplate>
编辑单元格模板
<DataTemplate x:Key="EditingPermissionTemplate">
<Border>
<UniformGrid Rows="1" Columns="1">
<ItemsControl ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=UserControl, Mode=FindAncestor}, Path=DataContext.AllPermissionLevels}" HorizontalContentAlignment="Stretch">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Button Style="{StaticResource BaseButtonStyle}"
Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl, Mode=FindAncestor}, Path=DataContext.UpdatePermissionCommand}"
CommandParameter="{Binding}" >
<TextBlock TextWrapping="Wrap" Text="{Binding Path=Access}" />
</Button>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</UniformGrid>
</Border>
</DataTemplate>
数据网格
<DataGrid ItemsSource="{Binding Path=AllUsersModules}" SelectedItem="{Binding Path=SelectedUsersModule}"
Style="{StaticResource BaseDataGridStyle}" SelectionUnit="FullRow">
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="{StaticResource WhiteColorBrush}" />
<Setter Property="Foreground" Value="Black" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Orange" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.CellStyle>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Module" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" Width="*"
CellTemplate="{StaticResource ModuleTemplate}"/>
<DataGridTemplateColumn Header="Permission Level" HeaderStyle="{StaticResource DataGridColumnHeaderStyle}" Width="*"
CellTemplate="{StaticResource PermissionTemplate}"
CellEditingTemplate="{StaticResource EditingPermissionTemplate}"/>
</DataGrid.Columns>
</DataGrid>
【问题讨论】:
-
是的,完全正确