XAML DataGridComboBoxColumn选择应用于Column中的所有组合框

6l7fqoea  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(107)

当我在DataGridComboBoxColumn内的ComboBox中进行选择时,该选择将应用于所有ComboBox。只有在设置DataGridComboBox的ElementStyle时才会出现这种情况。
有人知道我为什么会看到这种行为吗?
我已经附上了所有的代码需要重新创建这个问题下面。

视图模型

using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace WpfDotNetFramework472
{
    public class CompareSelectionViewModel
    {
        public CompareSelectionViewModel()
        {
            Items.Add(new ItemViewModel { ItemNumber = 1, ItemType = ItemType.None });
            Items.Add(new ItemViewModel { ItemNumber = 2, ItemType = ItemType.NewItem });
            Items.Add(new ItemViewModel { ItemNumber = 3, ItemType = ItemType.OldItem });
            Items.Add(new ItemViewModel { ItemNumber = 4, ItemType = ItemType.BrokenItem });
        }

        public ObservableCollection<ItemViewModel> Items { get; } = new ObservableCollection<ItemViewModel>();

        public IEnumerable<ItemType> AllowedItems { get; } = new ObservableCollection<ItemType>
        {
            ItemType.None,
            ItemType.BrokenItem,
            ItemType.NewItem,
            ItemType.OldItem
        };
    }
public class ItemViewModel : INotifyPropertyChanged
    {
        private int itemNumber;
        private ItemType itemType;

        public event PropertyChangedEventHandler PropertyChanged;

        public int ItemNumber 
        {
            get => itemNumber;
            set
            {
                itemNumber = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ItemNumber)));
            }
        }

        public ItemType ItemType 
        { 
            get => itemType;
            set
            {
                itemType = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(ItemType)));
            }
        }
    }
public enum ItemType
    {
        None,
        NewItem,
        OldItem,
        BrokenItem
    }
}

View.xaml

<UserControl x:Class="WpfDotNetFramework472.CompareSelection"
             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" 
             xmlns:local="clr-namespace:WpfDotNetFramework472"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <Grid>
        <DataGrid 
          AutoGenerateColumns="False"
          ItemsSource="{Binding Items}"
          Width="600">

            <DataGrid.Resources>
                <CollectionViewSource x:Key="ItemTypes" Source="{Binding AllowedItems}" />
            </DataGrid.Resources>

            <DataGrid.Columns>
                <DataGridTextColumn Header="Item Number"
                            Binding="{Binding ItemNumber}"
                            IsReadOnly="True" />
                <DataGridComboBoxColumn Header="Item Type"
                                ItemsSource="{Binding Source={StaticResource ItemTypes}}"
                                SelectedItemBinding="{Binding ItemType, UpdateSourceTrigger=PropertyChanged}">
                    <DataGridComboBoxColumn.ElementStyle>
                        <Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}">
                        </Style>
                    </DataGridComboBoxColumn.ElementStyle>
                </DataGridComboBoxColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</UserControl>

View.xaml.cs

using System.Windows.Controls;

namespace WpfDotNetFramework472
{
    public partial class CompareSelection : UserControl
    {
        public CompareSelection()
        {
            InitializeComponent();

            DataContext = new CompareSelectionViewModel();
        }
    }
}

问题似乎出在设置DataGridComboBoxColumn的ElementStyle时。如果注解掉设置样式的XAML部分,则可以单独设置ComboBox。

qnakjoqk

qnakjoqk1#

我找到了一个解决办法。按以下方式替换ElementStyle可解决此问题。

<DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="{x:Type ComboBox}" BasedOn="{x:Static DataGridComboBoxColumn.DefaultElementStyle}"/>
</DataGridComboBoxColumn.ElementStyle>

我仍然不知道为什么使用另一种样式会出错,但我认为这是DataGridComboBoxColumn的一个实现怪癖。

相关问题