无法更改 DataGridTextColumn 中的前景

发布于 2024-11-16 15:45:10 字数 1333 浏览 2 评论 0原文

所以,这是我的数据网格,

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

我使用以下属性将列表绑定到数据网格

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);

但这没有用。 所以请有人帮助我^^

So, this is my 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>

I´m binding a List to the Datagrid with following Properties

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

Now I want to make an Vocabulary Exam. The first column is filled with words and the other Column have to be filled with the translation.
My only problem is, I can´t change the foreground of the wrong translations, which the user have typed.
After the user have filled the grid, he have to click on a button, which will check if everything is correct. The wrong words have to turn in a red color.

I have tried

MyVocabularyExam[i].LanguageColor = Brushes.Red;

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

But that didn´t work.
So please someone help me ^^

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小镇女孩 2024-11-23 15:45:10

我必须显式设置 ElementStyle 才能使其工作:

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

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

I had to set ElementStyle explicitly to make it work:

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

Maybe there is a better solution, but I stopped searching at this point.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文