2011-06-22 26 views
1

所以,這是我的Datagrid在DataGridTextColumn無法更改前景

<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綁定列表到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);

但是沒有奏效。 所以,請幫助我的人^^

+0

查看http://stackoverflow.com/questions/1745132/how-do-i-change-the-background-color-of-a-cell-using-wpf-toolkit-datagrid – Mikhail

回答

6

我必須設置ElementStyle明確,使其工作:

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

也許有更好的解決辦法,但我此時停止搜索。

+0

我一直在尋找這個回答3整天....謝謝alesterre! – Danielle