我有以下问题而且我不知道我的错在哪里。 我使用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();
}
答案 0 :(得分:0)
有没有办法在不使用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>