以编程方式显示行详细信息

发布于 2024-09-13 14:08:23 字数 3958 浏览 2 评论 0原文

我有一个带有 SelectionUnit="Cell"DataGrid (官方的)。当用户选择一个单元格时,我想显示相应行的行详细信息。这显然不是默认行为,我似乎不知道如何实现这一点。

这是我的 XAML:

<UserControl x:Class="View.Test"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../AppResources.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <DataGrid AutoGenerateColumns="False" 
                ItemsSource="{Binding Path=Fields}" 
                BorderBrush="Transparent" 
                HeadersVisibility="Column"
                SelectionMode="Single"
                SelectionUnit="Cell"
                CanUserAddRows="False"
                CanUserDeleteRows="False"
                CanUserSortColumns="False"
                IsTextSearchEnabled="True"
                x:Name="EntryGrid"
                >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" Width="Auto" IsReadOnly="True"/>
                <DataGridTemplateColumn Header="Value" Width="Auto" x:Name="valueColumn" MinWidth="60" MaxWidth="90">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=DisplayValue}" TextTrimming="CharacterEllipsis" ToolTip="{Binding Path=Text, RelativeSource={RelativeSource Self}}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <DockPanel>
                                <ComboBox TabIndex="1" HorizontalAlignment="Stretch" VerticalAlignment="Center" SelectedValue="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="Value" ItemsSource="{Binding Path=FieldOptions}" Visibility="{Binding Path=FieldOptions, Converter={StaticResource EmptyCollectionIsInvisibleConverter}}" />
                                <TextBox TabIndex="2" Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" HorizontalAlignment="Stretch" Visibility="{Binding Path=FieldOptions, Converter={StaticResource NonEmptyCollectionIsInvisibleConverter}}"/>
                            </DockPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Description" IsReadOnly="True" Width="*" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
            <DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Help}" />
                </DataTemplate>
            </DataGrid.RowDetailsTemplate>
        </DataGrid>
    </Grid>
</UserControl>

I have a DataGrid (the official one) with SelectionUnit="Cell". When the user selects a cell, I'd like to show the row details for the corresponding row. This is apparently not the default behavior, and I can't seem to figure out how to accomplish this.

Here's my XAML:

<UserControl x:Class="View.Test"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="../AppResources.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </UserControl.Resources>
    <Grid>
        <DataGrid AutoGenerateColumns="False" 
                ItemsSource="{Binding Path=Fields}" 
                BorderBrush="Transparent" 
                HeadersVisibility="Column"
                SelectionMode="Single"
                SelectionUnit="Cell"
                CanUserAddRows="False"
                CanUserDeleteRows="False"
                CanUserSortColumns="False"
                IsTextSearchEnabled="True"
                x:Name="EntryGrid"
                >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" Width="Auto" IsReadOnly="True"/>
                <DataGridTemplateColumn Header="Value" Width="Auto" x:Name="valueColumn" MinWidth="60" MaxWidth="90">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=DisplayValue}" TextTrimming="CharacterEllipsis" ToolTip="{Binding Path=Text, RelativeSource={RelativeSource Self}}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                    <DataGridTemplateColumn.CellEditingTemplate>
                        <DataTemplate>
                            <DockPanel>
                                <ComboBox TabIndex="1" HorizontalAlignment="Stretch" VerticalAlignment="Center" SelectedValue="{Binding Path=Value, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="Value" ItemsSource="{Binding Path=FieldOptions}" Visibility="{Binding Path=FieldOptions, Converter={StaticResource EmptyCollectionIsInvisibleConverter}}" />
                                <TextBox TabIndex="2" Text="{Binding Value, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" HorizontalAlignment="Stretch" Visibility="{Binding Path=FieldOptions, Converter={StaticResource NonEmptyCollectionIsInvisibleConverter}}"/>
                            </DockPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellEditingTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Description" IsReadOnly="True" Width="*" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Description}" TextWrapping="Wrap" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
            <DataGrid.RowDetailsTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Path=Help}" />
                </DataTemplate>
            </DataGrid.RowDetailsTemplate>
        </DataGrid>
    </Grid>
</UserControl>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沙沙粒小 2024-09-20 14:08:23

您可以将 SelectionUnit 设置为 FullRow

SelectionUnit="FullRow"

,也可以处理选择事件并设置事件处理的可见性。

    private void EntryGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    {
        foreach (var cell in e.AddedCells)
        ((DataGridRow)EntryGrid.ItemContainerGenerator.ContainerFromItem(cell.Item)).DetailsVisibility = System.Windows.Visibility.Visible;
        foreach (var cell in e.RemovedCells)
            ((DataGridRow)EntryGrid.ItemContainerGenerator.ContainerFromItem(cell.Item)).DetailsVisibility = System.Windows.Visibility.Collapsed;
    }

在此插入有关 WPF DataGrid 的贬低言论。

You can set the SelectionUnit to FullRow

SelectionUnit="FullRow"

or you can handle the selection event and set the visibility on event handling.

    private void EntryGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    {
        foreach (var cell in e.AddedCells)
        ((DataGridRow)EntryGrid.ItemContainerGenerator.ContainerFromItem(cell.Item)).DetailsVisibility = System.Windows.Visibility.Visible;
        foreach (var cell in e.RemovedCells)
            ((DataGridRow)EntryGrid.ItemContainerGenerator.ContainerFromItem(cell.Item)).DetailsVisibility = System.Windows.Visibility.Collapsed;
    }

Insert disparaging remarks about the WPF DataGrid here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文