繁体   English   中英

以编程方式更改 WPF 按钮背景图像

[英]Change WPF button background image programmatically

我正在尝试使用 png 图像作为背景和背景颜色创建一个<Button/>

注意: png 图片来源可能还不知道,所以我不能把它们放在模板中。

我有一个看起来像这样的Style

<Style x:Key="MyButton" TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border CornerRadius="0">
                    <Image Margin="0" Source="/MyApp;component/Images/my-image.png" Stretch="None" />
                    <Border.Style>
                        <Style TargetType="{x:Type Border}">
                            <Setter Property="Background" Value="LimeGreen" />
                        </Style>
                    </Border.Style>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这给了我一个在纯 LimeGreen 背景上带有 png 图像的按钮。

我的MainWindow.xaml看起来有点像这样:

<Button Style="{StaticResource MyButton}" Click="btnMine_Click" /* some other props */ />

而它背后的代码是这样的:

private void btnMine_Click(object sender, RoutedEventArgs e)
{
    // TODO: change the image src on this button
    var myButton = sender as Button;

    // This won't work:
    // myButton.Border.Image.Source = getNewImageString();
}

如何将图像源从/MyApp2;component/Images/my-image.png为其他内容?

唉,在 Windows 窗体中,它实际上是一行。 在 WPF 中,它看起来像是几百个。

那是因为它是唯一可以放在 winforms 按钮中的东西。 WPF 是一个真正的 UI 框架,而不是一些随机的、半弃用的恐龙,它只允许做默认的东西(顺便说一下,这看起来很可怕)。

选项 1:将图像放置为按钮的Content

如果您只想在按钮中放置一个图像,而别无其他,为什么不这样做呢?

        <Style TargetType="Button">
            <Setter Property="Background" Value="LimeGreen"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Background="{TemplateBinding Background}">
                            <ContentPresenter ContentSource="Content"/>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

然后:

     <Button>
        <!-- Background not set, defaults to what's set in the Style -->
        <Image Source="./ChessPieces/BlackKnight.png"/>
    </Button>
    <Button Background="Red">
        <Image Source="./ChessPieces/BlackBishop.png"/>
    </Button>
    <Button Background="Blue">
        <Image Source="./ChessPieces/BlackPawn.png"/>
    </Button>

    <!-- WPF's idea of "Dynamic" is not the same as win(hack)forms' -->
    <Button Background="White">
        <Image Source="{Binding SomeStringPropertyDefinedInAViewModel}"/>
    </Button>

结果:

在此处输入图片说明

选项 2(不是很优雅):使用Tag属性:

如果除了 Image 之外,您还想在 Button 中放置其他东西,您可以采用一种有点老套的方法,将 ImageSource 放在 Button 的Tag属性中。

        <Style TargetType="Button">
            <Setter Property="Background" Value="LimeGreen"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Background="{TemplateBinding Background}">
                            <StackPanel Orientation="Horizontal">
                                <Image Source="{Binding Tag, RelativeSource={RelativeSource TemplatedParent}}"
                                       Height="50" Width="50"/>
                                <ContentPresenter ContentSource="Content"/>
                            </StackPanel>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

然后:

    <Button Tag="./ChessPieces/BlackKnight.png"
            Background="Blue"
            Foreground="White"
            Content="Some Text"/>

    <Button Tag="./ChessPieces/BlackBishop.png"
            Background="LightGray">
        <CheckBox Content="A CheckBox!" VerticalAlignment="Center"/>
    </Button>

结果:

在此处输入图片说明

选项 3:使用附加属性

与选项 2 相同,但使用在别处声明的属性,例如:

 <Button ButtonImage.Source="./ChessPieces/BlackBishop.png"
         <!-- etc -->

选项 4:创建自定义控件:

创建一个派生自 Button 的类(没有 XAML 的 .cs 文件)并添加一个DependencyProperty来保存图像,然后将模板设置为该类并使用该值。

选项 5:MVVM

创建一个ButtonViewModel ,或者实际使用在 ViewModel 中声明的DelegateCommand来绑定 Button 的属性。

不是一个选项:遍历可视化树并在代码中更改Image.Source

这不是你想做的事情。 这根本不是一个好方法。

我可以一直走下去,但我必须去睡觉。 如果您想让我详细说明这些方法中的任何一种,请告诉我。

想到了几种方法:

  1. 在运行时访问样式并遍历它以找到图像
  2. 在运行时给图像一个名称和引用,只需更改源

我认为最好的解决方案是创建一个用户控件并将图像源作为依赖属性,请参见: http : //www.codeproject.com/Articles/224230/Exploring-the-use-of-Dependency-Properties-in-User

这样,您将拥有一个现成的“自定义”控件,它支持您所追求的内容,并允许您直接引用它并使用绑定,这样您就可以避免使用样式的混乱解决方案。


需求讨论和扩展后,请参见:

如何从 C# 代码访问 wpf 中的 ResourceDictionary?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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