2010-03-13 90 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> 

这是您需要的示例,如果您的代码有效。 你也可以添加一个转换器来检查背景和其他属性。