views:

142

answers:

1

I have this in XAML in my style:

<DataTrigger Binding="{Binding Path=ButtonStyle}" Value="CutLeft">
  <DataTrigger.Setters>
    <Setter Property="CornerRadius" TargetName="border" Value="0 11 11 0" />
    <Setter Property="CornerRadius" TargetName="border1" Value="0 10 10 0" />
  </DataTrigger.Setters>
</DataTrigger>

And this XAML in my Window where i have the button:

<gui:MyCustomButton ButtonStyle="CutLeft"/>

And in MyCustomButton code:

public enum ButtonStyles {
    CutLeft, CutRight, Circular, Normal
}

public partial class MyCustomButton 
{
    [DefaultValue(ButtonStyles.Normal)]
    public ButtonStyles ButtonStyle { get; set; }
}

But it doesn't work! Any tips?

+2  A: 

I think there are a couple of problems here.

The first is that you're not raising property change notifications for ButtonStyle. Either change ButtonStyle to a dependency property, or implement INotifyPropertyChanged on MyCustomButton, and raise the PropertyChanged event from the ButtonStyle setter. In this case, the dependency property approach is probably better, because at some point someone is going to want to set ButtonStyle through a Style, and only DPs can be styled.

The second is that your data trigger appears to be looking at the data context, which is probably not the control. (If you look in the Output window, you'll probably be seeing binding errors about not being able to find the 'ButtonStyle' property on some data object.) You can get around this by adding RelativeSource={RelativeSource Self} to your DataTrigger. However, if you change ButtonStyle to a DP as suggested, you should just be able to use a plain old Trigger, which automatically works against the properties of the control being styled:

<Style.Triggers>
  <Trigger Property="ButtonStyle" Value="CutLeft">
    <Setter Property="CornerRadius" TargetName="border" Value="0 11 11 0" />
  </Trigger>
</Style.Triggers>

(Note also that you don't need to specify the Trigger.Setters or DataTrigger.Setters element. The XAML reader will fill that in for you.)

itowlson
Thank you, this was very helpful!
Erik