【问题标题】:Change ImageBrush Imagesource programmatically with conditions (WPF)使用条件 (WPF) 以编程方式更改 ImageBrush Imagesource
【发布时间】:2018-05-02 23:54:29
【问题描述】:

我有这样的xaml

<Window x:Class="Imagebind.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" Background="#FF3F3B51">
<Window.Resources>
    <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
        <Setter Property="FocusVisualStyle">
            <Setter.Value>
                <Style>
                    <Setter Property="Control.Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <Rectangle Margin="2" SnapsToDevicePixels="True" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </Setter.Value>
        </Setter>
        <Setter Property="Background" Value="#FFDDDDDD"/>
        <Setter Property="BorderBrush" Value="#FF707070"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" SnapsToDevicePixels="True">
                        <Border.Background>
                            <ImageBrush ImageSource="Cancel.bmp"/>
                        </Border.Background>
                        <ContentPresenter x:Name="contentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsDefaulted" Value="True">
                            <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                        </Trigger>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="Background" TargetName="border">
                                <Setter.Value>
                                    <ImageBrush ImageSource="Cancel (Mouseover).bmp"/>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="BorderBrush" TargetName="border" Value="#FFDDDDDD">
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter Property="Background" TargetName="border">
                                <Setter.Value>
                                    <ImageBrush ImageSource="Cancel (Pressed).bmp"/>
                                </Setter.Value>
                            </Setter>
                            <Setter Property="BorderBrush" TargetName="border" Value="#FF2C628B"/>
                        </Trigger>
                        <Trigger Property="ToggleButton.IsChecked" Value="True">
                            <Setter Property="Background" TargetName="border" Value="#FFBCDDEE"/>
                            <Setter Property="BorderBrush" TargetName="border" Value="#FF245A83"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="Background" TargetName="border" Value="#FFF4F4F4"/>
                            <Setter Property="BorderBrush" TargetName="border" Value="#FFADB2B5"/>
                            <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="#FF838383"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Button Content="" HorizontalAlignment="Left" Margin="174,226,0,0" VerticalAlignment="Top" Width="161" Height="24" Style="{DynamicResource ButtonStyle1}"/>

还有这样的c#代码

  using System.Windows;

namespace Imagebind
{
    public partial class MainWindow : Window
    {
        private string language;
        public MainWindow()
        {
    string language = "english";
            InitializeComponent();
            if (language == "english")
            {
            /* Here I need to change Imagesource of ImageBrush of 3 state buttons programmaticaly
For example this Cancel (Mouseover).bmp for Cancel(english) (Mouseover).bmp
*/
            }
        }
    }
}

所以我想要的是,如果用户使用不同的语言,界面中的所有图像都将使用我设置条件的语言。所以我需要在 c# 代码中将所有状态的按钮的所有图像更改为新的。

【问题讨论】:

  • 对 ImageSource 属性使用 IMultiValueConverter 怎么样?您可以将语言和默认图像传递给它,并让它根据语言进行转换。或者,您需要找到模板本身并对其进行修改,这并不像 imo 那样容易。如果走这条路,我建议将 ControlTemplate 从 Style 中移出,这样它就有自己的 x:Key 以便更容易从代码隐藏中找到。
  • 谢谢,但我对 c# wpf 的了解并不深。我认为它会是这样的: var myBrush = new ImageBrush(); myBrush.ImageSource = new BitmapImage(new Uri("pack://application:,,,/English/MainWindow.png", UriKind.Absolute)); mainwindow.Background = myBrush;但我可以将其应用于具有名称的控件。所以要从 c# 设置不同的源图像并不像我想象的那么容易?感谢您提供有关 IMultiValueConverter 的提示。伙计们,如果有人至少展示最小的代码示例如何更改图像,我将不胜感激。非常感谢您的帮助。

标签: c# wpf binding imagesource imagebrush


【解决方案1】:

如果您正在尝试本地化您的应用程序,您应该考虑查看现有的库来帮助您解决问题,例如 WPFLocalizationExtension

要回答您的直接问题,您可以将所有图像定义为静态属性或应用程序中某处的资源,并根据语言将它们分配给正确的值。如果您不需要支持在应用程序运行时更改语言,静态属性将更容易设置。

只需像这样定义属性:

internal static class Images
{
    public static Uri CancelMouseOver { get; set; }
}

像这样设置它们:

Images.CancelMouseOver = new Uri("Cancel(english) (Mouseover).bmp", UriKind.Relative);

然后像这样使用它们:

<ImageBrush ImageSource="{x:Static local:Images.CancelMouseOver}"/>

【讨论】:

  • 你救了我的命。 ) 太感谢了。我整天都在阅读有关绑定的信息。但这种方式更容易和简单。
猜你喜欢
  • 1970-01-01
  • 2017-08-11
  • 2015-10-03
  • 2018-12-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-05
  • 1970-01-01
相关资源
最近更新 更多