We have our different icons in form of xaml resources in the application something like this:
我们在应用程序中以xaml资源的形式提供了不同的图标,如下所示:
<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>
And we use these resources in another xaml file (load this resource in code behind, refer to the code below)
我们在另一个xaml文件中使用这些资源(在代码中加载此资源,请参阅下面的代码)
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; }
}
}
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)
问题是,我无法使用绑定设置图标颜色(在资源代码中,第一个代码片段),在ViewModel中具有属性(在这种情况下,MyBrush属性在Window2代码中)
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.
但这不起作用。我可能会在这里失踪。
1 个解决方案
#1
I've came up with two ways to resolve your problem.
我想出了两种解决问题的方法。
Solution I (better in my opinion)
解决方案I(在我看来更好)
Give up your code-behind approach and import your resource dictionary directly into your window resources and use StaticResourceExtension
to reference the resource:
放弃代码隐藏方法并将资源字典直接导入窗口资源,并使用StaticResourceExtension引用资源:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="TheNameOfYourDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
...
<Rectangle Fill="{StaticResource My_Icon}"/>
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}
.
当你这样做并使用AncestorType = local:Window2时你的绑定应该有效(虽然在当前形状中,对NewBrush属性的后续更改不会反映在绘图中 - 请参阅本答案末尾的列表)。请注意,必须使用{StaticResource My_Icon}而不是{Binding MyBrush}。
Solution II
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:
请注意,为了使此(或第一个解决方案)工作,应满足以下条件之一:
-
NewBrush
is set before the binding is set (beforeDataContext
is set in case of the first solution) -
Window2
implementsINotifyPropertyChanged
andPropertyChanged
event is raised afterNewBrush
is set -
NewBrush
is a dependency property
在设置绑定之前设置NewBrush(在第一个解决方案的情况下设置DataContext之前)
Window2实现了INotifyPropertyChanged,并且在设置NewBrush之后引发了PropertyChanged事件
NewBrush是一个依赖属性
#1
I've came up with two ways to resolve your problem.
我想出了两种解决问题的方法。
Solution I (better in my opinion)
解决方案I(在我看来更好)
Give up your code-behind approach and import your resource dictionary directly into your window resources and use StaticResourceExtension
to reference the resource:
放弃代码隐藏方法并将资源字典直接导入窗口资源,并使用StaticResourceExtension引用资源:
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="TheNameOfYourDictionary.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
...
<Rectangle Fill="{StaticResource My_Icon}"/>
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}
.
当你这样做并使用AncestorType = local:Window2时你的绑定应该有效(虽然在当前形状中,对NewBrush属性的后续更改不会反映在绘图中 - 请参阅本答案末尾的列表)。请注意,必须使用{StaticResource My_Icon}而不是{Binding MyBrush}。
Solution II
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:
请注意,为了使此(或第一个解决方案)工作,应满足以下条件之一:
-
NewBrush
is set before the binding is set (beforeDataContext
is set in case of the first solution) -
Window2
implementsINotifyPropertyChanged
andPropertyChanged
event is raised afterNewBrush
is set -
NewBrush
is a dependency property
在设置绑定之前设置NewBrush(在第一个解决方案的情况下设置DataContext之前)
Window2实现了INotifyPropertyChanged,并且在设置NewBrush之后引发了PropertyChanged事件
NewBrush是一个依赖属性