开发者

How to change buttonstyle in WPF, triggered by a boolean?

I need a Button which changes his开发者_开发百科 Backgroundimage everytime a boolean (IsFavourite) changes. I tried it with DataTrigger but he don´t know the property: "Source". Can you help me, I´m not that into xaml-code :-(

<Style x:Key="starButtonStyle" TargetType="{x:Type Button}">
    <Setter Property="IsEnabled" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Image x:Name="PART_img" Source="/FtpUploadClient;component/media/star_off_48.png" Height="28" Width="28" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="PART_img"
                        Property="Source"
                        Value="/FtpUploadClient;component/media/star_48.png" />
                    </Trigger>
                    <DataTrigger Binding="{Binding Path=IsFavourite}" Value="True">
                        <Setter Property="Source" Value="/FtpUploadClient;component/media/star_48.png" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Path=IsFavourite}" Value="False">
                        <Setter Property="Source" Value="/FtpUploadClient;component/media/star_off_48.png" />
                    </DataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

private void StarButton_Click(object sender, RoutedEventArgs e)
{
    IsFavourite = !IsFavourite;
}

private bool isFavourite = false;
public bool IsFavourite
{
    get { return isFavourite; }
    set
    {
        isFavourite = value;
        OnPropertyChanged("IsFavourite");
    }
}


public event PropertyChangedEventHandler PropertyChanged;

protected void OnPropertyChanged(string name)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null)
    {
        handler(this, new PropertyChangedEventArgs(name));
    }
}

-- The button-event Click calls a method that changes the IsFavourite:

<Button Content="Star" Style="{DynamicResource starButtonStyle}" Click="StarButton_Click" />


Add your Trigger to your Image Style instead of the ControlTemplate

<Image HorizontalAlignment="Center" VerticalAlignment="Center" Height="28" Width="28">
    <Image.Style>
        <Style TargetType="{x:Type Image}">
            <Setter Property="Source" Value="/FtpUploadClient;component/media/star_off_48.png" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Source" Value="/FtpUploadClient;component/media/star_48.png" />
                </Trigger>
                <DataTrigger Binding="{Binding Path=IsFavourite}" Value="True">
                    <Setter Property="Source" Value="/FtpUploadClient;component/media/star_48.png" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Image.Style>
</Image>


Button doesn't have a Source property.

You're trying to set the Source of the Image, by adding TargetName="PART_img" to the Setter.


1) Does your datacontext implement INotifyPropertyChanged, and is PropertyChanged("IsFavourite") raised on the setter? This will ensure that your view gets notified when that property changes.

2) try putting "Image.Source" instead of just "Source"

3) Try to use TargetName="PART_img" on the setter.


Solved it: With Rachels XAML and the property: this.DataContext = Classname; in the constructor it works :-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜