2014-07-18 2 views
0

테두리 색을 부울 메서드 IsToday 뒤에 코드를 바인딩하는 xaml과 C#을 사용하여 간단한 응용 프로그램을 만들었습니다. 하지만 어떻게 든 작동하지 않습니다.WPF xaml DataTrigger 바인딩이 작동하지 않습니다.

누군가 도움주세요. INotifyPropertyChanged도 시도했지만 작동하지 않습니다. 누군가가 도울 수 있다면 고마워, 고마워! 뒤에

코드 :

namespace WpfApplication3 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      IsToday = true; 
      InitializeComponent(); 
     } 

     public bool IsToday { get; set; } 
    } 
} 

XAML

<Window x:Class="WpfApplication3.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <ResourceDictionary Source="Dictionary1.xaml"> 
     </ResourceDictionary> 
    </Window.Resources> 
     <Grid> 
     <Border Style="{StaticResource Highlight}"> 
     </Border> 
    </Grid> 
</Window> 

XAML 사전

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
     <Style TargetType="Border" x:Key="Highlight"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding IsToday}" Value="True"> 
        <Setter Property="Background" Value="Red"/> 
       </DataTrigger> 
       <DataTrigger Binding="{Binding IsToday}" Value="False"> 
        <Setter Property="Background" Value="Blue"/> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </ResourceDictionary> 

답변

0

xaml에서 컨텍스트를 설정할 수 있습니다.

<Window x:Class="WpfApplication3.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525" 
     DataContext="{Binding RelativeSource={RelativeSource Self}}"> 
    <!-- your code --> 
</Window> 
+0

정말 고마워요! 이제 작동합니다. – user3854148

2

당신은 당신의 Window에 대한 DataContext을 설정해야합니다 :

public MainWindow() { 
     InitializeComponent(); 
     DataContext = this; 
     IsToday = true; 
} 

물론이 작업은 처음에는 일 때마다 변경됩니다. IsToday 이후에는 변경되지 않습니다. 아시다시피 우리는 INotifyPropertyChanged을 구현해야합니다.

+0

정말 고마워요! – user3854148

관련 문제