在 C# Wpf 应用程序中,如何动态更改用户控件中的按钮图标

问题描述 投票:0回答:1
c# wpf xaml icons pathgeometry
1个回答
0
投票

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>
© www.soinside.com 2019 - 2024. All rights reserved.