将DependancyProperty从Application资源绑定到Control DependancyProperty(即使用该资源)

时间:2017-10-26 02:18:14

标签: c# wpf user-interface data-binding custom-controls

我正在尝试构建一个从Image控件继承的自定义类,但我在绑定方面遇到了一些麻烦。

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

所以在我的App.xaml资源中,我有一些DrawingImages,例如:

l=list(zip(tp[0],tp[1]))
list(map(list, l))
Out[691]: [[0, 0], [0, 1], [0, 2]]

我已按如下方式构建自定义类:

<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:

 public class IconImage : Image
{
    public enum Icons
    {
        None,
        Shutdown,
        Minimize
    }

    private Icons _icon;
    public Icons Icon
    {
        get { return _icon; }
        set
        {
            _icon = value;
            if (value == Icons.None)
            {
                Visibility = Visibility.Hidden;
            }
            else
            {
                Visibility = Visibility.Visible;
                this.SetResourceReference(Image.SourceProperty, value.ToString());
            }
        }
    }

    public static readonly DependencyProperty ColourProperty = DependencyProperty.Register("Colour", typeof(Brush), typeof(IconImage), new PropertyMetadata(Brushes.Black));

    public Brush Colour
    {
        get { return (Brush)GetValue(ColourProperty); }
        set { SetValue(ColourProperty, value); }
    }
}

问题是我在DrawingImages中的画笔绑定不会绑定到自定义类中的我的Color DependancyProperty,我不知道如何完成此操作,或者,如果它甚至可能。如果我对刷子进行硬编码就可以完美地工作(显然)。我只需要能够在设计时或在后面的代码中随时更改控件的颜色。

提前致谢!

-Sean -

2 个答案:

答案 0 :(得分:0)

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

<DrawingImage x:Key="Shutdown" x:Shared="False">
    <!-- the rest -->
</DrawingImage>

然后不要使用SetResourceReference这样做:

var resource = FindResource(value.ToString());
// alternative
// var resource = Application.Current.Resources[value.ToString()];
this.Source = (ImageSource)resource;     

这与将DrawingImage xaml直接放在IconImage下的其他答案建议大致相同。

顺便说一句,您最好为资源添加更多唯一名称,以避免在树下意外使用具有相同名称的资源(特别是如果您要使用FindResource)。

答案 1 :(得分:-1)

{{1}}
相关问题