好吧,我想构建一个多语言系统,但是我有一些问题需要在imagesource中加载一个字典值。当我将变量绑定到文本框时,它会完美地显示该值,但同样不适用于imagesource,这里是我的代码:
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();
}
我在XAML中有这个
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" TargetName="border">
<Setter.Value>
<ImageBrush ImageSource="{Binding resourcesPath[exit_hover]}"/>
</Setter.Value>
答案 0 :(得分:1)
VisualBrush / ImageBrush不是元素树的一部分,因此它不会继承DataContext。
请试试这个:
<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>
在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();
}