我需要显示一个隐藏一些敏感信息的DataGrid,直到用户主动请求它为止。我这样构建了列:
<DataGrid.Columns>
<DataGridTextColumn Header="User Name" Binding="{Binding UserName}" IsReadOnly="False" MinWidth="90"/>
<DataGridTemplateColumn Header="Password" IsReadOnly="False" MinWidth="90">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="Show"
Command="{Binding Path=DataContext.ShowPassword, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding Path=SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridCheckBoxColumn Header="Owner Only" Binding="{Binding OwnerOnly}" IsReadOnly="{Binding IsOwner, Converter={StaticResource InverseBooleanConverter}}" MinWidth="90"/>
<DataGridCheckBoxColumn Header="Active" Binding="{Binding Active}" IsReadOnly="False" MinWidth="90"/>
<DataGridTextColumn Header="Created" Binding="{Binding Created, StringFormat=\{0:dd.MM.yyyy HH:mm\}}" IsReadOnly="True" MinWidth="90"/>
<DataGridTextColumn Header="Superseded" Binding="{Binding Superseded, StringFormat=\{0:dd.MM.yyyy HH:mm\}}" IsReadOnly="True" MinWidth="90"/>
产生这个:
当用户单击“显示”按钮时,会显示另一个视图,该视图会审核访问权限并允许他们更改密码。
我唯一的问题是我想允许他们添加新行。当他们这样做时,他们无法指定密码,因为该列是按钮而不是文本字段。
问题:无论如何将“新行”上的那个按钮更改为TextBox?
环境:MVVM在代码后面没有任何文件。如果这是唯一的方法,我不介意在代码中加入一些东西。
谢谢
接受的答案给了我90%的所需,但我不得不添加第二个数据触发器......
<ContentControl>
<ContentControl.Resources>
<Button x:Key="btn" Content="Show"
Command="{Binding Path=DataContext.ShowPassword, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding Path=SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"/>
</ContentControl.Resources>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="Content" Value="{StaticResource btn}" />
<Style.Triggers>
<DataTrigger Binding="{Binding Password}" Value="{x:Null}">
<Setter Property="Content">
<Setter.Value>
<TextBox />
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding DataContext, RelativeSource={RelativeSource AncestorType=DataGridRow}}" Value="{x:Static CollectionView.NewItemPlaceholder}">
<Setter Property="Content">
<Setter.Value>
<TextBox />
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
您可以使用绑定到父ContentControl
的DataContext
,如果它是TextBox
则显示NewItemPlaceholder
。像这样的东西:
<DataGridTemplateColumn Header="Password" IsReadOnly="False" MinWidth="90">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentControl>
<ContentControl.Resources>
<Button x:Key="btn" Content="Show"
Command="{Binding Path=DataContext.ShowPassword, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"
CommandParameter="{Binding Path=SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"/>
</ContentControl.Resources>
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="Content" Value="{StaticResource btn}" />
<Style.Triggers>
<DataTrigger Binding="{Binding DataContext, RelativeSource={RelativeSource AncestorType=DataGridRow}}" Value="{x:Static CollectionView.NewItemPlaceholder}">
<Setter Property="Content">
<Setter.Value>
<TextBox />
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>