2012-12-30 4 views
0

일부 값이있는 ListView가 있습니다. 나는 ListView의 글꼴 색상을 변경하려면 하지만,이 코드가 작동하지 않습니다 :부울 DataTrigger

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:sys="clr-namespace:System;assembly=mscorlib" 
    DataContext="{Binding RelativeSource={RelativeSource Self}}" 
    Title="Navi" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="603" Width="1029" 
    ShowInTaskbar="True" Closing="Window_Closing" Background="#FFD6D6D6"> 
<Window.Resources> 

    <Style TargetType="{x:Type TextBlock}"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding DarkTheme}" Value="True"> 
       <Setter Property="Control.Foreground" Value="Red"/> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 

CS :

public Int32 DarkTheme {get; set;} 
    //First init 
    public MainWindow() 
    { 
     DarkTheme = 1; 
     //Init component 

내가 뭘 잘못?

답변

1

DarkThemeInt32에서 bool으로 변경하십시오.

public bool DarkTheme {get; set;} 


DarkTheme = true; 

또는

또한 Binding에서 사용할 수있는 Converter을 만들 수 있습니다.

public class IntToBooleanConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
     if (value is int) 
     { 
      if ((int)value) == 1) 
       return true; 
      return false; 
     } 

     return DependencyProperty.UnsetValue; 
     } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (value is bool) 
     { 
      if ((bool)value)) 
       return 1; 
      return 0; 
     } 

     return DependencyProperty.UnsetValue; 
    } 
+0

미안하지만이 코드를 사용합니다. public bool DarkTheme {get; 설정;} DarkTheme = true; 그리고 문제가 해결되지 않습니다 – xnim

+1

'DarkTheme'은'MainWindow' 클래스가 아니라'DataContext'에 속해야합니다. 당신이 완전히 새로운 경우, [이] (http://rachel53461.wordpress.com/2012/07/14/what-is-this-datacontext-you-speak-of/) – Tilak

+0

당신이 옳다. 감사 – xnim