We have a ton of Buttons with an Icon and Text across the App, and I am trying to extract a common style for all of them. I came up with this idea of deriving from a Button and have a couple of dependency properties. This custom button class has a style common to all the Buttons in our Application.
My Custom Button definition:
<Button x:Class="UIElements.Controls.CustomToolBarButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" mc:Ignorable="d"
>
<!--Style="{StaticResource ResourceKey=ToolBarButtonStyle}"-->
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Icon, Mode=TwoWay}" Style="{StaticResource ResourceKey=ToolBarButtonIconStyle}" />
<TextBlock Text="{Binding DisplayText, Mode=TwoWay}" Style="{StaticResource ResourceKey=ToolBarButtonDisplayTextStyle}" />
</StackPanel>
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
//using Telerik.Windows.Controls;
namespace UIElements.Controls
{
public partial class CustomToolBarButton : Button
{
public CustomToolBarButton()
{
InitializeComponent();
this.DataContext = this;
}
public static readonly DependencyProperty IconProperty =
DependencyProperty.Register("Icon", typeof(BitmapImage), typeof(Button), new PropertyMetadata(default(BitmapImage)));
public BitmapImage Icon
{
get { return (BitmapImage)GetValue(IconProperty); }
set { SetValue(IconProperty, value); }
}
public static readonly DependencyProperty DisplayTextProperty =
DependencyProperty.Register("DisplayText", typeof(string), typeof(Button), new PropertyMetadata(default(string)));
public string DisplayText
{
get { return (string) GetValue(DisplayTextProperty); }
set { SetValue(DisplayTextProperty, value); }
}
}
}
And I am using this control as follows:
<Controls1:CustomToolBarButton Icon="{DynamicResource ImageSave}"
DisplayText="Test Display">
<Controls1:CustomToolBarButton.Style>
<Style TargetType="Controls1:CustomToolBarButton">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding SelectedListItem.ListId}" Value="0" />
</MultiDataTrigger.Conditions>
<!--<Setter Property="Button.IsEnabled" Value="False" />-->
<Setter Property="Background" Value="BurlyWood" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Controls1:CustomToolBarButton.Style>
Things look good, however the trigger is not firing when the bounded data changes. If I apply the same trigger to the regular button, it seems to be working fine.
Could you please tell me if I am missing something?
EDIT: Found an elegant solution. Made use of Attached Properties