加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

.net – 如何保持DataGridRow焦点和选择同步?

发布时间:2020-12-16 07:35:15 所属栏目:百科 来源:网络整理
导读:我有一个DataGrid,我希望所选行和焦点行同步,即如果焦点行发生更改,则所选行会发生变化,如果所选行发生更改,它将成为焦点行. 给定具有以下XAML的WPF窗口,如何同步聚焦和选定的行? Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.micro
我有一个DataGrid,我希望所选行和焦点行同步,即如果焦点行发生更改,则所选行会发生变化,如果所选行发生更改,它将成为焦点行.

给定具有以下XAML的WPF窗口,如何同步聚焦和选定的行?

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Grid>
        <Grid.Resources>

            <x:Array x:Key="MyList" Type="sys:String" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib">
                <sys:String>Hello</sys:String>
                <sys:String>World</sys:String>
                <sys:String>World</sys:String>
                <sys:String>World</sys:String>
                <sys:String>World</sys:String>
                <sys:String>World</sys:String>
                <sys:String>World</sys:String>
                <sys:String>World</sys:String>
                <sys:String>World</sys:String>
            </x:Array>

            <Style TargetType="{x:Type DataGrid}">
                <Setter Property="AlternationCount" Value="2" />
                <Setter Property="AutoGenerateColumns" Value="False"/>
            </Style>

            <Style TargetType="{x:Type DataGridCell}">
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="Focusable" Value="False"/>
            </Style>

            <Style TargetType="{x:Type DataGridRow}">
                <Setter Property="Focusable" Value="True"/>
                <Style.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="AlternationIndex" Value="0"/>
                            <Condition Property="IsSelected" Value="False"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" Value="White"/>
                    </MultiTrigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="AlternationIndex" Value="1"/>
                            <Condition Property="IsSelected" Value="False"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" Value="Gainsboro"/>
                    </MultiTrigger>
                    <Trigger Property="AlternationIndex" Value="1">
                        <Setter Property="Background" Value="Gainsboro"/>
                    </Trigger>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="#BF228B22"/>
                        <Setter Property="BorderBrush" Value="ForestGreen"/>
                        <Setter Property="BorderThickness" Value="1"/>
                    </Trigger>
                </Style.Triggers>
            </Style>

        </Grid.Resources>

        <DataGrid ItemsSource="{StaticResource MyList}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding}" Width="*"/>
            </DataGrid.Columns>
        </DataGrid>

    </Grid>

</Window>

使用下面的图像,您可以看到红色的焦点矩形和绿色的选定行明显不同步,因为我认为这是默认行为.我想要的是他们永远是同一个,即SelectedRow总是聚焦,FocusedRow总是被选中.

解决方法

尝试使用附加属性来监听DataGrid中的焦点更改和选择更改事件.当这些事情发生时,您可以通过更改选择或焦点来相应地做出反应.

这是一些示例代码.特别注意DataGridAttachment类.这就是定义附加属性的位置,以及逻辑在哪里对焦点和选择更改事件作??出反应.

MainWindow.xaml

<Window x:Class="_15098869.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:_15098869"
    Title="MainWindow"
    Width="525"
    Height="350">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <DataGrid Name="DataGrid"
              ItemsSource="{Binding Path=Items}"
              local:DataGridAttachment.SyncSelectionWithFocus="True" />
    <Button Grid.Row="1"
            Click="MoveSelectionButtonOnClick"
            Content="Move Selection" />
</Grid>

MainWindow.xaml.cs(代码隐藏)

using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace _15098869
{
    /// <summary>
    ///     Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();

            DataContext = this;

            Items = new ObservableCollection<DemoItem>();
            Items.Add(new DemoItem {FirstName = "John",LastName = "Doe"});
            Items.Add(new DemoItem {FirstName = "Jane",LastName = "Doe"});
            Items.Add(new DemoItem {FirstName = "Bob",LastName = "Doe"});
        }

        public ObservableCollection<DemoItem> Items { get; private set; }

        public void MoveSelectionButtonOnClick(object sender,RoutedEventArgs e)
        {
            var max = DataGrid.Items.Count - 1;
            DataGrid.SelectedIndex = DataGrid.SelectedIndex == max ? 0 : DataGrid.SelectedIndex + 1;
        }
    }

    public class DemoItem
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }

    public static class DataGridAttachment
    {
        public static readonly DependencyProperty SyncSelectionWithFocusProperty =
            DependencyProperty.RegisterAttached("SyncSelectionWithFocus",typeof (bool),typeof (DataGridAttachment),new PropertyMetadata(default(bool),SyncSelectionWithFocusChanged));

        private static void SyncSelectionWithFocusChanged(DependencyObject dependencyObject,DependencyPropertyChangedEventArgs e)
        {
            var dataGrid = dependencyObject as DataGrid;
            if (dataGrid == null) return;

            var shouldSync = GetSyncSelectionWithFocus(dataGrid);

            if (shouldSync)
            {
                dataGrid.AddHandler(UIElement.GotFocusEvent,new RoutedEventHandler(DataGridOnGotFocus));
                dataGrid.AddHandler(Selector.SelectionChangedEvent,new SelectionChangedEventHandler(DataGridOnSelectionChanged));
            }
        }

        private static void DataGridOnGotFocus(object sender,RoutedEventArgs e)
        {
            var dataGrid = sender as DataGrid;
            var element = e.OriginalSource as DataGridCell;

            if (dataGrid == null || element == null) return;
            dataGrid.SelectedItem = element.DataContext;
        }

        private static void DataGridOnSelectionChanged(object sender,SelectionChangedEventArgs e)
        {
            var dataGrid = sender as DataGrid;

            if (dataGrid == null) return;

            if (!dataGrid.IsKeyboardFocusWithin)
            {
                dataGrid.Focus();
            }
        }

        public static void SetSyncSelectionWithFocus(DataGrid element,bool value)
        {
            element.SetValue(SyncSelectionWithFocusProperty,value);
        }

        public static bool GetSyncSelectionWithFocus(DataGrid element)
        {
            return (bool) element.GetValue(SyncSelectionWithFocusProperty);
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读