【问题标题】:Setting ElementStyle of DataGridComboBoxColumn breaks expected behavior设置 DataGridComboBoxColumn 的 ElementStyle 会破坏预期的行为
【发布时间】:2017-09-08 03:13:12
【问题描述】:

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

查看错误行为的image。红色文本应与左侧相同。因为对象是相同的(参见 GUID)。

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

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

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

为了复制问题,我附上了源代码。

MainWindow.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>

MainWindow.xaml.cs

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

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

TestItem.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();
}

【问题讨论】:

    标签: c# .net wpf combobox datagrid


    【解决方案1】:

    有什么方法可以在不使用 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>
    

    【讨论】:

    • 谢谢,这似乎适用于简单的情况(例如前景),但是为ComboBox 指定ContentTemplate 怎么样?
    • 当 DataGridComboBoxColumn 的功能因任何原因不能满足您的需求时,请使用 DataGridTemplateColumn。
    • 请记住接受您的问题已解决的答案:meta.stackexchange.com/questions/23138/…
    猜你喜欢
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多