我目前有一个自定义Button
控件,它通过类型为DrawingImage
的依赖项属性在控件上显示ControlTemplate
集。我一直在尝试将DrawingImage
移动到控件模板中并绑定到Geometry
,但失败了。是否可以绑定到Geometry
对象?如果不是,为什么不,如果是,我错过了什么?
下面的屏幕截图显示了我在右侧的成功尝试和左侧的失败尝试。
绑定到DrawingImage(有效)的示例 - generic.xaml:
<Style TargetType="{x:Type local:RoundButton3}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:RoundButton3}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Ellipse StrokeThickness="5" Stroke="Green"/>
<Viewbox Margin="10" Stretch="Uniform">
<ContentControl
Template="{TemplateBinding ImageTemplate}" />
</Viewbox>
<TextBlock VerticalAlignment="Center"
Grid.Column="1"
Text="Round Button" Margin="5,0,0,0" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
与
public class RoundButton3 : Button
{
static RoundButton3()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(RoundButton3),
new FrameworkPropertyMetadata(typeof(RoundButton3)));
}
public static readonly DependencyProperty ImageTemplateProperty =
DependencyProperty.Register(
"ImageTemplate", typeof (ControlTemplate), typeof (RoundButton3),
new FrameworkPropertyMetadata(
default(ControlTemplate),
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.AffectsMeasure));
public ControlTemplate ImageTemplate
{
get { return (ControlTemplate)GetValue(ImageTemplateProperty); }
set { SetValue(ImageTemplateProperty, value); }
}
}
并像这样使用
<Grid.Resources>
<GeometryGroup x:Key="AddGeometry">
<LineGeometry StartPoint="25,12" EndPoint="25,38" />
<LineGeometry StartPoint="12,25" EndPoint="38,25" />
</GeometryGroup>
<ControlTemplate x:Key="RoundAddTemplate">
<Image SnapsToDevicePixels="True">
<Image.Source>
<DrawingImage>
<DrawingImage.Drawing>
<DrawingGroup>
<GeometryDrawing Pen="{StaticResource RoundButtonPen}" Geometry="{StaticResource AddGeometry}"/>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
</Image.Source>
</Image>
</ControlTemplate>
</Grid.Resources>
<local:RoundButton3
Grid.Column="1"
Content="RoundButton3" ImageTemplate="{StaticResource RoundAddTemplate}"/>
我尝试绑定到几何资源(不起作用) - generic.xaml:
<Style TargetType="{x:Type local:RoundButton2}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:RoundButton2}">
...
<Viewbox Margin="10" Stretch="Uniform">
<Image>
<Image.Source>
<DrawingImage>
<DrawingImage.Drawing>
<DrawingGroup>
<DrawingGroup.Children>
<GeometryDrawing
Pen="{StaticResource RoundButtonPen}"
Geometry="{TemplateBinding Geometry}" />
</DrawingGroup.Children>
</DrawingGroup>
</DrawingImage.Drawing>
</DrawingImage>
</Image.Source>
</Image>
</Viewbox>
...
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
其中
public class RoundButton2 : Button
{
static RoundButton2()
{
DefaultStyleKeyProperty.OverrideMetadata(
typeof(RoundButton2),
new FrameworkPropertyMetadata(typeof(RoundButton2)));
}
#region Geometry
public static readonly DependencyProperty GeometryProperty =
DependencyProperty.Register(
"Geometry", typeof (Geometry), typeof (RoundButton2),
new FrameworkPropertyMetadata(
default(Geometry),
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.AffectsMeasure));
public Geometry Geometry
{
get { return (Geometry)GetValue(GeometryProperty); }
set { SetValue(GeometryProperty, value); }
}
#endregion
}
并使用
<local:RoundButton2
Grid.Column="0"
Geometry="{StaticResource AddGeometry}"
Content="RoundButton2" />
使用之前定义的AddGeometry资源。
答案 0 :(得分:5)
如果替换
,您的代码应该有效<GeometryDrawing
Geometry="{TemplateBinding Geometry}" />
通过
<GeometryDrawing
Geometry="{Binding Geometry, RelativeSource={RelativeSource TemplatedParent}}" />
请参阅this question以获取解释。