2010-03-13 79 views
1

我在XAML有幾個按鈕的用戶控件....XAML用戶控件datatrigger

當我的C#代碼更改爲true「VideoEnable」屬性我想改變一個按鈕的顏色。

下面的代碼編譯,但崩潰,我無法找到一個合適的解決方案

<UserControl.Triggers> 
    <DataTrigger Binding="{Binding VideoEnable}" Value="true"> 
     <Setter Property="Button.Background" Value="Green" TargetName="VideoButton" /> 
     <Setter Property="Grid.Background" Value="Blue" TargetName="videoGrid" /> 
    </DataTrigger> 
</UserControl.Triggers> 

現在我已經用下面的代碼試過,它不會崩潰,但背景不會改變:■

<UserControl.Resources> 
    <Style TargetType="{x:Type Button}"> 
     <Style.Triggers> 
      <DataTrigger Binding="{Binding Path=Videos}" Value="true"> 
       <Setter Property="Button.Background" Value="Green" /> 
      </DataTrigger> 
     </Style.Triggers> 
    </Style> 
</UserControl.Resources> 

    public string Videos 
    { 
     get { return m_videos; } 

     set 
     { 
      m_videos = value; 
      NotifyPropertyChanged("Videos"); 
     } 
    } 

好吧,我發現這個問題...

這是我的按鈕

 <Button DataContext="{Binding LensesBtn}" Margin="0,5,0,0" FontSize="14" FontWeight="Bold" Height="40" Opacity="0.8" HorizontalAlignment="Stretch" Style="{StaticResource {x:Static ToolBar.ButtonStyleKey}}"> 
      <Button.Background>#dbebf9</Button.Background> 
      <Button.BorderBrush>PowderBlue</Button.BorderBrush> 
      <Button.BorderThickness>4</Button.BorderThickness> 
      Lenses 
     </Button> 

當我刪除的DataContext,風格和背景屬性所有的工作....

但我真的需要這個特性

任何提示?

+0

你能發佈錯誤信息嗎? – 2010-03-13 11:46:11

+0

應用程序只是崩潰。我認爲我以錯誤的方式使用dataTrigger:s? – Bert 2010-03-13 18:34:40

回答

0
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}"> 
    <Style.Triggers> 
     <Trigger Property="Videos" Value="True"> 
      <Setter Property="Background" Value="Green" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

這是您需要的示例,如果您的代碼有效。 你也可以添加一個轉換器來檢查背景和其他屬性。