使用包含如下路径的UserControl:
<UserControl x:Class="MyApp.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Path x:Name="path1" >
<Path.Data>
<GeometryGroup>
<EllipseGeometry x:Name="ellipse1" ... />
<EllipseGeometry x:Name="ellipse2" ... />
</GeometryGroup>
</Path.Data>
</Path>
</UserControl>
如何将Path的属性(如Fill或Stroke)公开为usercontrol的属性,以便我可以在它们上声明绑定。
<MineAllMine:MyUserControl ... DataContext="{Binding MyMasterPlan}" Fill="{Binding Colour}" />
我尝试通过声明新的DependencyProperties来包装属性:
public class MyUserControl:UserControl{
...
public static readonly DependencyProperty FillProperty =
DependencyProperty.Register("Fill", typeof(Brush), typeof(MyUserControl),
new PropertyMetadata(Path.FillProperty.DefaultMetadata.DefaultValue));
public Brush Fill
{
get { return path1.Fill; }
set { path1.Fill = value; }
}
...
}
唉,无济于事。
答案 0 :(得分:2)
您应该将Path.Fill绑定到UserControl上已有的DP,例如Background
或Foreground
,或者定义一个可以绑定到的新版本,例如PathFill
。最终结果类似于在ControlTemplate中使用TemplateBinding,除非您将RelativeSource绑定用于父UserControl。
public class MyUserControl : UserControl
{
public static readonly DependencyProperty PathFillProperty =
DependencyProperty.Register(...);
public Brush PathFill
{
get { return GetValue(...); }
set { SetValue(...); }
}
}
XAML:
<Path Fill="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=PathFill">
...
</Path>