Imageコントロールから継承したカスタムクラスを作成しようとしていますが、バインディングに問題があります。アプリケーションリソースからDependancyPropertyをコントロールDependancyProperty(リソースを使用している)にバインドする
私はいくつかのDrawingImages、1の例を持っている私の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>
と
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); }
}
}
Usasge:次のように私は私のカスタムクラスを構築している
<local:IconImage Icon="Shutdown" Colour="CornflowerBlue" />
DrawingImagesの私のBrushバインディングは、カスタムクラスのColor DependancyPropertyにバインドされず、これを実現する方法や、可能であれば考えられません。私がブラシをハードコードすると、それは完全に(明らかに)動作します。私は、デザイン時にコントロールから、またはコードの背後にあるいつでも、それらの色を変更できるようにする必要があります。
ありがとうございます!
-Sean-
'DrawingImage'はどこで使用されましたか? 'DrawingImage'に対して' local:IconImage'の 'AncestorType'を持っていますか? – Iron
Iconプロパティが設定されている場合に使用されます。this.SetResourceReference(Image.SourceProperty、value.ToString());この場合、DynamicReferenceを "Shutdown"に設定します。 –
'x:Key =" Shutdown "'のリソースを使用するコードを表示してください。 – Iron