无法在DataGridTextColumn中更改Foreground

时间:2011-06-22 13:03:37

标签: asp.net wpfdatagrid

所以,这是我的Datagrid

    <DataGrid AutoGenerateColumns="false" Height="270" HorizontalAlignment="Left" Name="dataGrid1" VerticalAlignment="Top" Width="503" ItemsSource="{Binding Path=MyVocabularyExam, Mode=TwoWay}" CanUserAddRows="False" CanUserResizeColumns="False" CanUserReorderColumns="False" CanUserSortColumns="False" SelectionMode="Single" SelectionUnit="Cell" >      
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="Sprache1" Width="*" Header="Sprache1" Binding="{Binding Language1}" IsReadOnly="True" />
            <DataGridTextColumn x:Name="Sprache2" Width="*" Header="Sprache2" Binding="{Binding Language2, Mode=TwoWay}" IsReadOnly="False" Foreground="{Binding LanguageColor}"/>        
        </DataGrid.Columns>    
    </DataGrid>

我使用以下属性将List绑定到Datagrid

public class myVocabulary
{
    public string Language1 { get; set; }
    public string Language2 { get; set; }
    public SolidColorBrush LanguageColor { get; set; }
}

现在我想做词汇考试。第一列填充了单词,另一列填充了翻译。 我唯一的问题是,我无法更改用户输入的错误翻译的前景。 在用户填满网格后,他必须单击一个按钮,这将检查一切是否正确。错误的单词必须变成红色。

我试过了

MyVocabularyExam [i] .LanguageColor = Brushes.Red;

MyVocabularyExam [i] .LanguageColor = new SolidColorBrush(Colors.Red);

但那不起作用。 所以请有人帮助我^^

1 个答案:

答案 0 :(得分:6)

我必须明确设置ElementStyle才能使其正常工作:

<DataGridTextColumn.ElementStyle>
    <Style TargetType="{x:Type TextBlock}">
         <Setter Property="Foreground"
                 Value="{Binding Path=FontColor}"></Setter>
    </Style>
</DataGridTextColumn.ElementStyle>

也许有更好的解决方案,但我此时停止了搜索。