2

I got such 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}"/>

And such c# code

  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
*/
            }
        }
    }
}

So all I wanted is that if user has different languages all images in interface will be in language that I setted up conditions. So I need to change all images of buttons in all states to new in c# code.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Jack Robinson
  • 65
  • 1
  • 5
  • See [this answer about setting imagesource of an image](https://stackoverflow.com/questions/10146269/set-imagesource-of-wpf-image?rq=1) – Amadeusz Wieczorek Jun 17 '15 at 15:47
  • What about using an IMultiValueConverter for your ImageSource property? You could pass it the language and the default image, and have it convert it based on the language. Alternatively, you need to find the template itself and modify that, which is not as easy to do imo. If going that route, I would suggest moving the ControlTemplate out of the Style so it has it's own x:Key to make it easier to find from the code-behind. – Rachel Jun 17 '15 at 16:11
  • Thank you but I am not so deep in c# wpf. I thought it would be something like: var myBrush = new ImageBrush(); myBrush.ImageSource = new BitmapImage(new Uri("pack://application:,,,/English/MainWindow.png", UriKind.Absolute)); mainwindow.Background = myBrush; But I can apply this on control that have a name. So to set different sourceimages from c# it's not so easy as I thought ? Thank you for the tip about IMultiValueConverter. Guys I would be appreciate if someone show at least smallest code example how to change images. Many thanks for your help. – Jack Robinson Jun 17 '15 at 18:04

1 Answers1

2

If you are trying to localize your application, you should consider looking into existing libraries to help you out, such as WPFLocalizationExtension.

To answer your immediate question, you could define all of your images as either static properties or as resources somewhere in your application, and assign them to the correct values based on the language. If you don't need to support changing the language while the application is running, static properties will be easier to setup.

Simply define properties like this:

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

Set them like this:

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

Then use them like this:

<ImageBrush ImageSource="{x:Static local:Images.CancelMouseOver}"/>
Xavier
  • 3,254
  • 15
  • 22
  • You saved my life. ) Thank you so much. I just was reading about bindings all day. But this way much easier and simple. – Jack Robinson Jun 17 '15 at 22:16