views:

32

answers:

1

My attempt (below) fails:

<Canvas x:Key="Lock" ... />
<Canvas x:Key="Unlock" ... />

<Style x:Key="LockButtonStyle" TargetType="{x:Type Button}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=IsReadOnly}" Value="True">
            <DataTrigger.Setters>
                <Setter Property="Content" Value="{StaticResource Lock}" />                           
            </DataTrigger.Setters>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=IsReadOnly}" Value="False">
            <DataTrigger.Setters>
                <Setter Property="Content" Value="{StaticResource Unlock}" />
            </DataTrigger.Setters>
        </DataTrigger>
    </Style.Triggers>
</Style>

...

<Button Content="{StaticResource Lock}" />

I'm trying to get the button to change when the IsReadOnly property on the ViewModel changes (it fires INotifyPropertyChanged.PropertyChanged with "IsReadOnly" as the PropertyName). What am I missing?

.NET 3.5

ANSWER (at least for me - doesn't support the general case):

I just wrote a quick converter for boolean property binding.

[ValueConversion(typeof(bool), typeof(object))]
public class BooleanValueConverter : IValueConverter
{
    public object FalseValue { get; set; }
    public object TrueValue { get; set; }

    #region IValueConverter Members

    public object Convert(object value, Type targetType, 
                          object parameter, CultureInfo culture)
    {
        return (bool)value ? this.TrueValue : this.FalseValue;
    }

    public object ConvertBack(object value, Type targetType, 
                              object parameter, CultureInfo culture)
    {
        return object.Equals(this.TrueValue, value) ? true : false;
    }

    #endregion
}

...

<local:BooleanValueConverter x:Key="LockOrUnlock" 
    TrueValue="{StaticResource Unlock}" 
    FalseValue="{StaticResource Lock}" />

...

<Button Content="{Binding Path=IsReadOnly, 
                             Converter={StaticResource LockOrUnlock}}" />
A: 

If you set the property "Content" of the Button, you cannot change it with a trigger, because the first one takes precedence.
Try to remove the setting of Content and it should work, because triggers will do the right work themselves.

Maurizio Reginelli
What do you mean by "it should work"? Setting Content is what I'm trying to accomplish. If you cannot change it with a trigger, then I don't see how this is possible at all.
Travis Heseman
Your trigger sets the content in any case, depending from the value of IsReadOnly property. You don't need to set content in your button. Give to it the style LockButtonStyle and see what happens.
Maurizio Reginelli