【问题标题】:C# bind dictionary in wpf XAMLwpf XAML中的C#绑定字典
【发布时间】:2017-05-06 21:43:57
【问题描述】:

好吧,我想构建一个多语言系统,但是在图像源中加载字典值时遇到了一些问题。当我将变量绑定到文本框时,它会完美地显示该值,但同样不适用于图像源,这是我的代码:

        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>

【问题讨论】:

    标签: c# wpf xaml dictionary


    【解决方案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();            
    }
    

    【讨论】:

    • 对象引用未设置为对象的实例。
    • 因为你在设置 DataContext 之后初始化了你的公共值。您应该在设置 DataContext 之前初始化您的字典。有几种方法可以做到这一点,我用一个建议扩展了我的帖子。
    • 很高兴能帮上忙
    猜你喜欢
    • 2011-02-28
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 2021-01-16
    • 2018-12-09
    • 2011-02-05
    • 1970-01-01
    • 2011-03-27
    相关资源
    最近更新 更多