Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式更改WPF按钮背景图像_C#_Wpf_Xaml - Fatal编程技术网

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

C# 以编程方式更改WPF按钮背景图像,c#,wpf,xaml,C#,Wpf,Xaml,我正在尝试创建一个背景为png图像和背景颜色的 注意:png图像源可能还不知道,所以我不能把它们放在模板中 我有一个样式,看起来像这样: <Style x:Key="MyButton" TargetType="Button"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button">

我正在尝试创建一个背景为png图像和背景颜色的

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

我有一个
样式
,看起来像这样:

<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>
<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();
}
它背后的代码是这样的:

<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>
<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
到其他东西?

我想到了几种方法:

  • 在运行时访问样式并遍历它以查找图像
  • 在运行时为映像指定一个名称和引用,只需更改源即可
  • 我认为最好的解决方案是创建一个usercontrol,并将图像源作为一个依赖属性,请参见:

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


    在对要求进行讨论和扩展后,请参见:

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

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

    选项1:将图像作为按钮的
    内容放置

    如果您只想在按钮中放置一个图像,而不想其他任何东西,为什么不这样做呢

            <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>
    
    然后:

    
    
    结果:

    选项3:使用

    与选项2相同,但使用其他地方声明的属性,例如:

     <Button ButtonImage.Source="./ChessPieces/BlackBishop.png"
             <!-- etc -->
    
    
    
    p、 手动编辑一个.xaml文件不是程序化的,这里似乎有几个答案,让人觉得是这样的:-/你想要它完全像那样(你的点击处理程序)?你能改变XAML吗?只有当你无法“触摸”该样式时,才能将其设置为“全部程序化”。当然,我可以编辑该样式,但按钮太多,无法实现。然而,所有的按钮都有相同的背景色。不,不完全一样,这只是一个例子。我想做的是在某些事件(单击、鼠标输入、窗口大小调整等)后,拍摄一个
    按钮
    对象并更改其背景图像。有意义吗?将单击触发事件添加到按钮并更改模板中的背景
    在运行时访问样式并遍历它以找到图像
    ,这正是我想要做的,但我不知道如何定义。@davidmudoch样式在哪里?这将完全影响您在运行时访问它的方式,遍历将比简单地创建usercontrol更为棘手。它位于名为“Templates.xaml”(ResourceDictionary)的文件中,该文件在我的
    App.xaml
    中引用。我可能误解了您,我不想访问
    样式本身,因为它将应用于许多按钮。我只想在特定按钮上覆盖样式的“属性”(或在xaml中调用的任何属性)。@DavidMurdoch这是不可能的,唯一具有特定按钮覆盖的方法是使用UserControl。我使用了类似于“选项1:将图像作为按钮内容放置”的内容。谢谢但是我没有用按钮;我不知道如何设计按钮的悬停和按下状态。