简体   繁体   中英

How can I change WPF Button Image in Setter

I have a very strange problem trying to change the controltemplate for a button in WPF (c#).

I now have the following code:

    <Window.Resources>
    <Style x:Key="ButtonFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="Black" StrokeThickness="1" StrokeDashArray="1 2"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="MacOSX-Close" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border Background="Black" BorderThickness="0">
                    <Image x:Name="bgimg" Source="bin/debug/Ressources/Images/GUI/frame-btn-defaulted.png"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="#ADADAD"/>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="true">
                            <Setter TargetName="bgimg" Property="Source" Value="bin/debug/Ressources/Images/GUI/frame-btn-defaulted.png"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

So this isn't working. It says

    "{DependencyProperty.UnsetValue}" is no valid value for property "System.Windows.Controls.Image.Source" on a setter.

And when I tried using multiple images in a grid (because you can only set one child) and modified the "visibility" property, the button didn't show up at all!!! No button, no image. And, yes, I am sure the image exists. But the source is relative to the solution root, not to the compiled program... does this matter? Please help me :) I'm gettin' little desperate here... :(

Best regards ;) And please ask if somethin's not clear to you.

Create folder like say Resources in your project and put image in your resources folder and their build action should be set to Resource. Then you can set Source = "/Resources/frame-btn-defaulted.png"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM