Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将DependencyProperty从应用程序资源绑定到控件DependencyProperty(即使用该资源)_C#_Wpf_User Interface_Data Binding_Custom Controls - Fatal编程技术网

C# 将DependencyProperty从应用程序资源绑定到控件DependencyProperty(即使用该资源)

C# 将DependencyProperty从应用程序资源绑定到控件DependencyProperty(即使用该资源),c#,wpf,user-interface,data-binding,custom-controls,C#,Wpf,User Interface,Data Binding,Custom Controls,我试图构建一个从映像控件继承的自定义类,但绑定方面遇到了一些问题 我在我的资源中使用DrawingImage(这是为矢量图像提供的) 因此,在我的App.xaml资源中,我有一些DrawingImage,例如: <DrawingImage x:Key="Shutdown"> <DrawingImage.Drawing> <DrawingGroup> <Draw

我试图构建一个从映像控件继承的自定义类,但绑定方面遇到了一些问题

我在我的资源中使用DrawingImage(这是为矢量图像提供的)

因此,在我的App.xaml资源中,我有一些DrawingImage,例如:

<DrawingImage x:Key="Shutdown">
            <DrawingImage.Drawing>
                <DrawingGroup>
                    <DrawingGroup.Children>
                        <DrawingGroup>
                            <DrawingGroup.Children>
                                <GeometryDrawing Brush="{x:Null}">
                                    <GeometryDrawing.Pen>
                             Here-->    <Pen Brush="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:IconImage}}, Path=Colour, UpdateSourceTrigger=PropertyChanged}" Thickness="2" />
                                    </GeometryDrawing.Pen>
                                    <GeometryDrawing.Geometry>
                                        <PathGeometry FillRule="Nonzero" Figures="M8.332,4.941C5.759,6.271 4,8.956 4,12.052 4,16.47 7.582,20.052 12,20.052 16.418,20.052 20,16.47 20,12.052 20,8.911 18.19,6.193 15.555,4.884" />
                                    </GeometryDrawing.Geometry>
                                </GeometryDrawing>
                       Here-->  <GeometryDrawing Brush="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:IconImage}}, Path=Colour, UpdateSourceTrigger=PropertyChanged}" Pen="{x:Null}">
                                    <GeometryDrawing.Geometry>
                                        <RectangleGeometry RadiusX="0" RadiusY="0" Rect="11,2,2,10" />
                                    </GeometryDrawing.Geometry>
                                </GeometryDrawing>
                            </DrawingGroup.Children>
                            <DrawingGroup.ClipGeometry>
                                <RectangleGeometry Rect="0,0,24,24" />
                            </DrawingGroup.ClipGeometry>
                        </DrawingGroup>
                    </DrawingGroup.Children>
                </DrawingGroup>
            </DrawingImage.Drawing>
        </DrawingImage>
Usasge:

<local:IconImage Icon="Shutdown" Colour="CornflowerBlue" />

问题是DrawingImages中的笔刷绑定不会绑定到自定义类中的颜色相关属性,我不知道如何实现这一点,或者,如果可能的话。如果我硬编码的刷子,它的工作完美(显然)。我只需要能够改变颜色的控制在设计时,或在任何时候在代码背后

提前谢谢

-肖恩-



您可以让它像这样工作。首先将资源标记为非共享,因为您希望使您的
DrawingImage
根据它所属的
IconImage
更改外观,这对共享资源没有多大意义(在本例中,共享意味着与单例类似-只有一个全局实例):

这与将您的
DrawingImage
xaml直接放在
IconImage
下大致相同,就像其他答案所建议的那样


顺便说一句,您最好为您的资源提供更多唯一的名称,以避免意外使用树下同名的资源(特别是如果您要使用
FindResource

你可以让它像这样工作。首先将资源标记为非共享,因为您希望使您的
DrawingImage
根据它所属的
IconImage
更改外观,这对共享资源没有多大意义(在本例中,共享意味着与单例类似-只有一个全局实例):

这与将您的
DrawingImage
xaml直接放在
IconImage
下大致相同,就像其他答案所建议的那样


顺便说一句,您最好为您的资源提供更多唯一的名称,以避免意外使用树下同名的资源(特别是如果您要使用
FindResource

在哪里使用了
DrawingImage
?对于
DrawingImage
,确保具有
local:IconImage
AnteStorType
?设置图标属性时使用它:this.SetResourceReference(Image.SourceProperty,value.ToString());这将在本例中设置为“Shutdown”的DynamicReference。请在IconImage类中显示使用资源
x:Key=“Shutdown”
?的代码:设置“Icon”属性时,它将图像源设置为引用x:Key=“Shutdown”资源,因为我正在将图标设置为“Shutdown”。这是一个枚举,我只使用ToString(),因为我的枚举值与x:Key名称完全匹配。在哪里使用了
DrawingImage
?对于
DrawingImage
,确保具有
local:IconImage
AnteStorType
?设置图标属性时使用它:this.SetResourceReference(Image.SourceProperty,value.ToString());这将在本例中设置为“Shutdown”的DynamicReference。请在IconImage类中显示使用资源
x:Key=“Shutdown”
?的代码:设置“Icon”属性时,它将图像源设置为引用x:Key=“Shutdown”资源,因为我正在将图标设置为“Shutdown”。这是一个枚举,我只使用ToString(),因为我的枚举值与x:Key名称完全匹配。我确信这可以工作,但这不允许我动态设置源,我不希望每次使用此控件时都必须指定整个DrawingImage。(它被多次使用)并且有时在运行时会发生更改,这就是为什么我有Icon属性。让
图标
成为
依赖属性
并对
图标
使用
样式
来获得您想要的东西怎么样?我相信这可以奏效,但这不允许我动态设置源代码,我不希望每次使用此控件时都必须指定整个DrawingImage。(它被使用了很多次)有时它在运行时会改变,这就是为什么我有Icon属性。让
图标
<local:IconImage Icon="Shutdown" Colour="CornflowerBlue" />
<local:IconImage Icon="Shutdown" Colour="CornflowerBlue">
    <local:IconImage.Source>
        <DrawingImage>
            <DrawingImage.Drawing>
                <DrawingGroup>
                    <DrawingGroup.Children>
                        <DrawingGroup>
                            <DrawingGroup.Children>
                                <GeometryDrawing Brush="{x:Null}">
                                    <GeometryDrawing.Pen>
                                        <Pen Brush="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:IconImage}}, Path=Colour, UpdateSourceTrigger=PropertyChanged}" Thickness="2" />
                                    </GeometryDrawing.Pen>
                                    <GeometryDrawing.Geometry>
                                        <PathGeometry FillRule="Nonzero" Figures="M8.332,4.941C5.759,6.271 4,8.956 4,12.052 4,16.47 7.582,20.052 12,20.052 16.418,20.052 20,16.47 20,12.052 20,8.911 18.19,6.193 15.555,4.884" />
                                    </GeometryDrawing.Geometry>
                                </GeometryDrawing>
                                <GeometryDrawing Brush="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:IconImage}}, Path=Colour, UpdateSourceTrigger=PropertyChanged}" Pen="{x:Null}">
                                    <GeometryDrawing.Geometry>
                                        <RectangleGeometry RadiusX="0" RadiusY="0" Rect="11,2,2,10" />
                                    </GeometryDrawing.Geometry>
                                </GeometryDrawing>
                            </DrawingGroup.Children>
                            <DrawingGroup.ClipGeometry>
                                <RectangleGeometry Rect="0,0,24,24" />
                            </DrawingGroup.ClipGeometry>
                        </DrawingGroup>
                    </DrawingGroup.Children>
                </DrawingGroup>
            </DrawingImage.Drawing>
        </DrawingImage>
    </local:IconImage.Source>
</local:IconImage>
<DrawingImage x:Key="Shutdown" x:Shared="False">
    <!-- the rest -->
</DrawingImage>
var resource = FindResource(value.ToString());
// alternative
// var resource = Application.Current.Resources[value.ToString()];
this.Source = (ImageSource)resource;