我想在选中复选框后显示rowdetails,如果未选中则将其折叠。我也不想使用代码隐藏。我知道转换器存在解决方案。对于事件触发,我正在使用

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属性。而且DataGrids没有任何DataGrid属性。 DetailsVisibilityDetailsVisibility的属性。

如果我是你,我会忘记EventTriggers的,而是使用常规的DataGridRow

还没有尝试过,但是类似的东西应该可以工作:

<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>

关于c# - 数据网格行详细信息,无需代码,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32712472/

10-11 07:10