2010-02-11 72 views
12

如何爲StackPanel定義DataTrigger?它有一個Trigger屬性,但在啓動應用程序的時候,這裏定義觸發給出了初始化以下錯誤:爲StackPanel定義DataTrigger

失敗的對象初始化(ISupportInitialize.EndInit)。觸發器集合成員必須是EventTrigger類型。 ....

這是從以下幾個簡單的DataTrigger給出:

<StackPanel x:Name="PersonPanel" DataContext="{Binding CurrentPerson}"> 
    <TextBlock Text="{Binding Id}" /> 
    <TextBlock Text="{Binding Name}" /> 
    <StackPanel.Triggers> 
     <DataTrigger Binding="{Binding Id}" Value="1"> 
      <Setter TargetName="PersonPanel" Property="StackPanel.Background" Value="Green" /> 
     </DataTrigger>     
    </StackPanel.Triggers> 
</StackPanel> 

這是我第Trigger過,所以我該怎麼辦錯了嗎?我應該在其他地方定義Trigger嗎?

回答

11

嘗試是這樣的

<Window.Resources> 
     <Style x:Key="spStyle" TargetType="StackPanel"> 
      <Setter Property="StackPanel.Background" Value="Red" /> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding Id}" Value="1"> 
        <Setter Property="StackPanel.Background" Value="Green" /> 
       </DataTrigger> 
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 
    <StackPanel x:Name="PersonPanel" Style="{StaticResource spStyle}" DataContext="{Binding CurrentPerson}"> 
     <TextBlock Text="{Binding Id}" /> 
     <TextBlock Text="{Binding Name}" /> 
    </StackPanel> 
+0

Thx!完美的作品:-) – stiank81

7

試試這個:

<TextBlock Text="{Binding Id}"> 
    <TextBlock.Style> 
     <Style> 
      <Style.Triggers> 
       <Trigger Property="TextBlock.Text" Value="1"> 
        <Setter TargetName="PersonPanel" Property="StackPanel.Background" Value="Green" /> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </TextBlock.Style> 
</TextBlock> 
+1

THX!這工作,但我與費薩爾的解決方案.. – stiank81

+0

不客氣。 –