I have a UserControl in XAML with a couple of buttons....
When the "VideoEnable" property in my C# code change to true I want to change the color of a button.
The following code compiles but crashes and I can't find a right solution
<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>
Now i have tried with the following code, it does not crash but the background does not change :s
<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");
}
}
Ok, I found the problem...
This is my button
<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>
When I delete the DataContext, Style and Background properties it all works....
But i really need this properties开发者_运维技巧
any tips?
<Style x:Key="ButtonStyle" TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="Videos" Value="True">
<Setter Property="Background" Value="Green" />
</Trigger>
</Style.Triggers>
</Style>
this is the example you need if your code is valid. you can also add a converter in order to check the background and other properties.
精彩评论