c# - 设置 DataGridComboBoxColumn 的 ElementStyle 会破坏预期的行为

标签 c# .net wpf combobox datagrid

我遇到了以下问题,我不知道我的错在哪里。 我指定了一个带有 DataGridComboBoxColumn 的 DataGrid 并设置了一个简单的 ElementStyle,它应该为非编辑模板的文本着色。着色有效,但文本值本身始终是最后编辑的值。

参见 image的错误行为。红色文本应与左侧相同。由于对象相同(请参阅 GUID)。

我尝试将基本样式设置为 {x:Static DataGridComboBoxColumn.TextBlockComboBoxStyleKey} 并尝试将 TargetType 设置为 DataGridComboBoxColumn+TextBlockComboBox(通过反射)。没有改变任何东西。

我通过 Live Property Explorer 注意到,DataGridComboBoxColumn+TextBlockComboBox 的 SelectedItem 是针对左网格而不是右网格进行评估的。所以我的假设是绑定(bind)会在设置样式时中断。

这是 WPF DataGrid 的已知问题吗?有什么方法可以在不使用 DataGridTemplateColumn 的情况下解决这个问题吗?

为了重现这个问题,我附上了源代码。

主窗口.xaml

<Window x:Class="DataGridComboBoxStyleError.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DataGridComboBoxStyleError"
        mc:Ignorable="d"
        Title="MainWindow" Height="200" Width="750" WindowStartupLocation="CenterScreen">
    <Window.Resources>
        <CollectionViewSource x:Key="_availableCultures" Source="{Binding Source={x:Static local:MainWindow.AvailableCultures}}" />

        <x:Array x:Key="Items" Type="{x:Type local:TestItem}">
            <local:TestItem CultureName="de" />
            <local:TestItem CultureName="en" />
            <local:TestItem CultureName="ru" />
        </x:Array>

        <Style x:Key="_comboBoxStyle" x:Shared="False" TargetType="{x:Type ComboBox}">
            <Setter Property="Foreground" Value="Red" />
        </Style>

    </Window.Resources>
    <StackPanel>
        <Grid x:Name="_mainGrid">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="auto" />
                <RowDefinition Height="*" />
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0" Grid.ColumnSpan="2" Text="ItemsSource via StaticResource" HorizontalAlignment="Center" />
            <TextBlock Grid.Row="1" Grid.Column="0" Text="Default ElementStyle" HorizontalAlignment="Center" />
            <TextBlock Grid.Row="1" Grid.Column="1" Text="Specified ElementStyle" HorizontalAlignment="Center" />
            <DataGrid Grid.Row="2" Grid.Column="0" ItemsSource="{StaticResource Items}" CanUserAddRows="False" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Id" Binding="{Binding Id}" />
                    <DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource _availableCultures}}" SelectedItemBinding="{Binding Culture}" DisplayMemberPath="NativeName" Header="Culture" Width="*" />
                </DataGrid.Columns>
            </DataGrid>
            <DataGrid Grid.Row="2" Grid.Column="1" ItemsSource="{StaticResource Items}" CanUserAddRows="False" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="Id" Binding="{Binding Id}" />
                    <DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource _availableCultures}}" SelectedItemBinding="{Binding Culture}" DisplayMemberPath="NativeName" Header="Culture" Width="*" ElementStyle="{StaticResource _comboBoxStyle}" />
                </DataGrid.Columns>
            </DataGrid>
        </Grid>
    </StackPanel>
</Window>

主窗口.xaml.cs

namespace DataGridComboBoxStyleError
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public static IEnumerable<CultureInfo> AvailableCultures = CultureInfo.GetCultures(CultureTypes.AllCultures);
    }
}

测试项.cs

public class TestItem
{
    public CultureInfo Culture { get; set; }

    public string CultureName
    {
        get {
            return Culture.TwoLetterISOLanguageName;
        }
        set {
            Culture = CultureInfo.GetCultureInfo(value);
        }
    }

    public string Id { get; } = Guid.NewGuid().ToString();
}

最佳答案

Is there any way to resolve this issue without using a DataGridTemplateColumn?

您可以设置 CellStyle 而不是设置 ElementStyle:

<DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource _availableCultures}}" SelectedItemBinding="{Binding Culture}" DisplayMemberPath="NativeName" Header="Culture" Width="*">
    <DataGridComboBoxColumn.CellStyle>
        <Style TargetType="{x:Type DataGridCell}">
            <Setter Property="Foreground" Value="Red" />
        </Style>
    </DataGridComboBoxColumn.CellStyle>
</DataGridComboBoxColumn>

关于c# - 设置 DataGridComboBoxColumn 的 ElementStyle 会破坏预期的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43367840/

相关文章:

c# - 如何从现有的 Windows 服务启动 AspNetCore 应用程序

c# - 使用托管服务帐户从 IIS 将文件写入网络驱动器

c# - 属性描述符如何使用相同的代码行获取两个控件的值?

.net - 不同配置的 ClickOnce 部署

c# - TextBox.Clear() 或 TextBox.Text = string.Empty

c# - 如何禁用整个系统的空气震动?

c# - 为什么 C# 没有为变量和方法设计 'const'?

c# - 如何访问 ASP.NET Core 服务中的路由数据/值提供程序数据?

.net - 有没有人有在 Visual Studio 中创建自定义文本标记的示例?

c# - 如何拥有宽度可调的固定列的自定义 Datagrid 控件?