UserControl 应公开一个 依赖属性
public static readonly DependencyProperty IconDataProperty =
DependencyProperty.Register(
nameof(IconData), typeof(Geometry), typeof(TaskView));
public Geometry IconData
{
get => (Geometry)GetValue(IconDataProperty);
set => SetValue(IconDataProperty, value);
}
通过
RelativeSource
绑定在控件的 XAML 中的 Path 元素中使用。请注意,不需要 Viewbox。
<Button Width="40" Height="40" ...>
<Path Width="22" Height="22"
Data="{Binding IconData,
RelativeSource={RelativeSource AncestorType=UserControl}}"
Fill="{StaticResource OnAccent}"
Stretch="Uniform"/>
</Button>