Reputation: 5
Well, i want to build a multi-language system, but im having some problem to load a dictionary value inside the imagesource. When i bind the variabe to a textbox, it shows the value perfectly, but the same doesnt works for imagesource, here its my code:
Dictionary<string, string> resource = new Dictionary<string, string>()
{
{ "play_ready", "Resources/" + Config.Language + "play_ready.png" },
{ "play_click", "Resources/" + Config.Language + "play_click.png" },
{ "background", "Resources/" + Config.Language + "background.png" },
{ "exit", "Resources/" + Config.Language + "exit.png" },
{ "exit_click", "Resources/" + Config.Language + "exit_click.png" },
{ "exit_hover", "../../../Resources/exit_hover.png" }
};
public Dictionary<string, string> resourcesPath { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
resourcesPath = resource;
Config.Setup(this.configUrl);
this.client = new Client();
}
and i have that in the XAML
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="border">
<Setter.Value>
<ImageBrush ImageSource="{Binding resourcesPath[exit_hover]}"/>
</Setter.Value>
Upvotes: 0
Views: 963
Reputation: 783
A VisualBrush/ImageBrush is not part of the element tree so it doesn't inherit the DataContext.
Try this please:
<Window ...
x:Name="window">
<Window.Resources>
<Image x:Key="img" Source="{Binding resourcesPath[exit_hover], Source={x:Reference window}}"/>
</Window.Resources>
...
<Setter Property="Background">
<Setter.Value>
<VisualBrush Visual="{StaticResource img}" />
<!-- or use ImageBrush like this. -->
</Setter.Value>
</Setter>
In xaml.cs
Dictionary<string, string> resource;
public Dictionary<string, string> resourcesPath {
get{ if(resource == null)
{ resource = new Dictionary<string, string>() {
{ "play_ready", "Resources/" + Config.Language + "play_ready.png" },
{ "play_click", "Resources/" + Config.Language + "play_click.png" },
{ "background", "Resources/" + Config.Language + "background.png" },
{ "exit", "Resources/" + Config.Language + "exit.png" },
{ "exit_click", "Resources/" + Config.Language + "exit_click.png" },
{ "exit_hover", "../../../Resources/exit_hover.png" }
};
}
return resource;}
}
public MainWindow()
{
InitializeComponent();
DataContext = this;
Config.Setup(this.configUrl);
this.client = new Client();
}
Upvotes: 1