WPF:无法通过绑定在资源中设置颜色 [英] WPF: Not able to set color in resource through binding

查看:28
本文介绍了WPF:无法通过绑定在资源中设置颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在应用程序中以 xaml 资源的形式拥有不同的图标,如下所示:

We have our different icons in form of xaml resources in the application something like this:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<DrawingBrush x:Key="My_Icon">
    <DrawingBrush.Drawing>
        <GeometryDrawing Brush="Gray"> <!--I want to set this Brush here using binding-->
            <GeometryDrawing.Geometry>
                <GeometryGroup>
                    <EllipseGeometry Center="50,50" RadiusX="45" RadiusY="20" />
                    <EllipseGeometry Center="50,50" RadiusX="20" RadiusY="45" />
                </GeometryGroup>
            </GeometryDrawing.Geometry>
        </GeometryDrawing>
    </DrawingBrush.Drawing>
</DrawingBrush>

并且我们在另一个xaml文件中使用这些资源(在后面的代码中加载这个资源,参考下面的代码)

And we use these resources in another xaml file (load this resource in code behind, refer to the code below)

public partial class Window2 : Window
{
    public Window2()
    {
        InitializeComponent();

        var resource = Application.Current.FindResource("My_Icon");
        this.MyBrush = resource as DrawingBrush;

        NewBrush = Brushes.Blue;

        this.DataContext = this;
    }

    private DrawingBrush _myBrush;

    public DrawingBrush MyBrush
    {
        get { return _myBrush; }
        set { _myBrush = value; }
    }

    private Brush _newBrush;

    public Brush NewBrush
    {
        get { return _newBrush; }
        set { _newBrush = value; }
    }
}

问题是,我无法使用绑定设置图标颜色(在资源代码中,第一个代码片段),其属性位于 ViewModel 中(本例中为 MyBrush 属性,在 Window2 代码后面)

The issue is, I am not able to set the icon color (in resource code, the first code snippet) using the binding, with property which is in ViewModel (MyBrush property in this case in Window2 code behind)

我尝试在资源文件中使用以下代码:

I tried with following code in resource file:

<GeometryDrawing Brush="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Rectangle}, Path=NewBrush}">

但这不起作用.我可能在这里遗漏了什么.

But this isn't working. What I might be missing here.

推荐答案

我想出了两种方法来解决您的问题.

I've came up with two ways to resolve your problem.

解决方案一(我认为更好)

放弃你的代码隐藏方法,将你的资源字典直接导入到你的窗口资源中,并使用StaticResourceExtension来引用资源:

Give up your code-behind approach and import your resource dictionary directly into your window resources and use StaticResourceExtension to reference the resource:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="TheNameOfYourDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>
...
<Rectangle Fill="{StaticResource My_Icon}"/>

当您这样做并使用 AncestorType=local:Window2 时,您的绑定应该可以工作(尽管在当前形状中,对 NewBrush 属性的后续更改不会反映在绘图中- 请参阅本答案末尾的列表).请注意,必须使用 {StaticResource My_Icon} 而不是 {Binding MyBrush}.

When you do that and use AncestorType=local:Window2 your binding should work (although in current shape subsequent changes to NewBrush property won't be reflected in the drawing - refer to the list at the end of this answer). Note that it is necessary to use {StaticResource My_Icon} instead of {Binding MyBrush}.

方案二

在代码隐藏中设置绑定:

Set the binding in your code-behind:

var resource = Application.Current.FindResource("My_Icon");
this.MyBrush = resource as DrawingBrush;

NewBrush = Brushes.Blue;

BindingOperations.SetBinding(MyBrush.Drawing, GeometryDrawing.BrushProperty,
    new Binding { Path = new PropertyPath("NewBrush"), Source = this });

请注意,为了使此(或第一个解决方案)起作用,应满足以下条件之一:

Note that in order for this (or the first solution) to work one of these conditions should be met:

  1. NewBrush 在设置绑定之前设置(在第一种解决方案的情况下设置 DataContext 之前)
  2. Window2 实现 INotifyPropertyChangedPropertyChanged 事件在 NewBrush 设置后引发
  3. NewBrush 是一个依赖属性
  1. NewBrush is set before the binding is set (before DataContext is set in case of the first solution)
  2. Window2 implements INotifyPropertyChanged and PropertyChanged event is raised after NewBrush is set
  3. NewBrush is a dependency property

这篇关于WPF:无法通过绑定在资源中设置颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆