【问题标题】:WPF: DataGrid.RowDetailsTemplate: Only show details if single row is selected?WPF:DataGrid.RowDetailsTemplate:仅在选择单行时显示详细信息?
【发布时间】:2014-07-15 04:23:15
【问题描述】:

我在我的DataGrids 之一中使用RowDetailsTemplate。到目前为止,这工作得很好,但是当用户想要为特定操作选择多行时看起来真的很奇怪。 有没有一种简单的方法来显示RowDetailsTemplate,仅在仅选择一行时显示?

我很想用纯 XAML 解决这个问题。否则我会用后面的代码来做:

private void DataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DataGrid temp = sender as DataGrid;

    if (temp.SelectedItems.Count == 1)
    {
        temp.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.VisibleWhenSelected;
    }
    else
    {
        temp.RowDetailsVisibilityMode = DataGridRowDetailsVisibilityMode.Collapsed;
    }
}

【问题讨论】:

    标签: wpf xaml datagrid rowdetailstemplate


    【解决方案1】:

    DataGrid 有一个属性RowDetailsVisibilityMode。选择多个行时将其设置为折叠 em>。 您的 XAML 应该类似于

    <DataGrid Name="dataGrid1" RowDetailsVisibilityMode="{Binding Path=SelectedItems.Count, RelativeSource={RelativeSource Self}, Converter={StaticResource rdtvc}}">
    

    和相应的转换器类似

    public class Converter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
    
            if (value != null && (int)value == 1)
                return DataGridRowDetailsVisibilityMode.VisibleWhenSelected;
            else 
                return DataGridRowDetailsVisibilityMode.Collapsed;
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
    

    【讨论】:

    • ...最好的方法是什么?我很想在 XAML 中做到这一点,但我想不出一个合适的方法来做到这一点......
    • 哇...对我有用。谢谢!
    【解决方案2】:

    根据 user1994514 的回答,我开发了一个仅 XAML 的版本,无需转换器。如果有人想避免额外的转换器,您可以使用带有数据触发器的样式来实现相同的效果。

        <DataGrid>
            <DataGrid.Style>
                <Style TargetType="DataGrid">
                    <Setter Property="RowDetailsVisibilityMode" Value="Collapsed" />
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding SelectedItems.Count, RelativeSource={RelativeSource Self}}" Value="1">
                            <Setter Property="RowDetailsVisibilityMode" Value="VisibleWhenSelected" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DataGrid.Style>
        </DataGrid>
    

    【讨论】:

      猜你喜欢
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 2018-10-25
      • 1970-01-01
      • 1970-01-01
      • 2016-07-05
      • 2016-03-24
      • 1970-01-01
      相关资源
      最近更新 更多