数据网格行细节没有代码开始
本文关键字:代码 开始 细节 数据网 网格 数据 | 更新日期: 2023-09-27 18:13:57
我想在选中复选框后显示行详细信息,如果未选中则折叠它。我也不想使用代码隐藏。我知道存在带转换器的解决方案。对于事件触发器,我使用
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
但是它不起作用。选中复选框后,行详细信息不起作用。我认为视觉树中不同分支的问题。
namespace WpfApplication9
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new Data();
}
}
public class Emloyee
{
public string FirstName{ get; set; }
public string LastName { get; set; }
public string ID { get; set; }
public double Sallary { get; set; }
}
public class Data
{
public ObservableCollection<Emloyee> Items { get; set; }
public Data()
{
Items = new ObservableCollection<Emloyee>();
Items.Add(new Emloyee { FirstName="Jhon",ID="1",LastName="Smith",Sallary=100});
Items.Add(new Emloyee { FirstName="Neo",ID="0",LastName="Neo",Sallary=0});
}
}
}
Xaml
<Window.Resources>
<SolidColorBrush x:Key="PrimaryBrush" Color="White" />
<SolidColorBrush x:Key="PrimaryFont" Color="Black" />
</Window.Resources>
<Grid>
<DataGrid x:Name="ShowGrid"
ColumnWidth="*"
EnableRowVirtualization="False"
VerticalGridLinesBrush="Transparent"
HorizontalGridLinesBrush="Transparent"
ItemsSource="{Binding Items}"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserResizeRows="False"
AutoGenerateColumns="False"
CanUserSortColumns="False"
RowDetailsVisibilityMode="Collapsed">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="{StaticResource PrimaryBrush}"/>
<Setter Property="Foreground" Value="{StaticResource PrimaryFont}" />
<Setter Property="FontSize" Value="10"/>
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.RowDetailsTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Sallary}"></Label>
</StackPanel>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
<DataGrid.Columns>
<DataGridTemplateColumn Width="10">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<Label HorizontalContentAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" VerticalContentAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox VerticalAlignment="Center" HorizontalAlignment="Center">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<ei:ChangePropertyAction TargetName="ShowGrid" PropertyName="DetailsVisibility" Value="Visible"/>
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<ei:ChangePropertyAction TargetName="ShowGrid" PropertyName="DetailsVisibility" Value="Collapsed"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<Label Content="First Name" HorizontalContentAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" VerticalContentAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding FirstName}" HorizontalAlignment="Center" VerticalContentAlignment="Center" FontSize="10"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="*" >
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<Label Content="ID" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding ID}" HorizontalAlignment="Center" VerticalContentAlignment="Center" FontSize="10"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Width="*" >
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<Label Content="Last Name" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Label Content="{Binding LastName}" HorizontalAlignment="Center" VerticalContentAlignment="Center" FontSize="10"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
在你的触发器中:
<i:EventTrigger EventName="Checked">
<ei:ChangePropertyAction TargetName="ShowGrid" PropertyName="DetailsVisibility" Value="Visible"/>
</i:EventTrigger>
首先,CheckBox.Checked
不是一个事件,它是一个布尔属性。该事件被称为CheckedChanged
,但它不适用于您的触发器。
另外,你试图改变名为"ShowGrid"的目标的DetailsVisibility
属性…也就是DataGrid
。datagrid没有任何DetailsVisibility
属性。DetailsVisibility
是DataGridRow
的属性。
如果我是你,我会忘记EventTriggers,而使用常规的Binding
。
还没有尝试过,但是像这样的东西应该可以工作:
<Window.Resources>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
</Window.Resources>
<!-- ... -->
<DataGridTemplateColumn Width="10">
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<Label HorizontalContentAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Center" VerticalContentAlignment="Center"/>
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox VerticalAlignment="Center" HorizontalAlignment="Center"
Checked="{Binding DetailsVisibility,
Mode=TwoWay,
RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}},
Converter={StaticResource BooleanToVisibilityConverter}}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>