猿问

创建 WPF 控件以显示矢量图标

我正在尝试从XAML源代码的材料设计中学习。

这是他们的 GitHub:

这是我正在研究的代码:

这是 PackIcon 的辅助类:

目前,我正在查看他们的图标包示例并对其进行快速测试。

这是测试类:


public class PackIconTest : Control

{

    static PackIconTest()

    {

        DefaultStyleKeyProperty.OverrideMetadata(typeof(PackIconTest), new FrameworkPropertyMetadata(typeof(PackIconTest)));

    }


    public PackIconTest()

    {

        Data = "M4.93,4.93C3.12,6.74 2,9.24 2,12C2,14.76 3.12,17.26 4.93,19.07L6.34,17.66C4.89,16.22 4,14.22 4,12C4,9.79 4.89,7.78 6.34,6.34L4.93,4.93M19.07,4.93L17.66,6.34C19.11,7.78 20,9.79 20,12C20,14.22 19.11,16.22 17.66,17.66L19.07,19.07C20.88,17.26 22,14.76 22,12C22,9.24 20.88,6.74 19.07,4.93M7.76,7.76C6.67,8.85 6,10.35 6,12C6,13.65 6.67,15.15 7.76,16.24L9.17,14.83C8.45,14.11 8,13.11 8,12C8,10.89 8.45,9.89 9.17,9.17L7.76,7.76M16.24,7.76L14.83,9.17C15.55,9.89 16,10.89 16,12C16,13.11 15.55,14.11 14.83,14.83L16.24,16.24C17.33,15.15 18,13.65 18,12C18,10.35 17.33,8.85 16.24,7.76M12,10A2,2 0 0,0 10,12A2,2 0 0,0 12,14A2,2 0 0,0 14,12A2,2 0 0,0 12,10Z";

    }


    private static readonly DependencyPropertyKey DataPropertyKey =

        DependencyProperty.RegisterReadOnly(nameof(Data), typeof(string), typeof(PackIconTest), new PropertyMetadata(""));


    public static readonly DependencyProperty DataProperty = DataPropertyKey.DependencyProperty;


    [TypeConverter(typeof(GeometryConverter))]

    public string Data

    {

        get { return (string)GetValue(DataProperty); }

        private set { SetValue(DataPropertyKey, value); }

    }

}

这是 XAML 用法:


<local:PackIconTest Width="200" Height="200"/>

图标不显示。我错过了什么?


一只斗牛犬
浏览 273回答 1
1回答

犯罪嫌疑人X

我通过为 PackIconTest 目标类型创建一个样式来解决这个问题。这是xaml代码:&nbsp; &nbsp; <Window.Resources>&nbsp; &nbsp; <Style TargetType="local:PackIconTest">&nbsp; &nbsp; &nbsp; &nbsp; <Setter Property="Template">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Setter.Value>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <ControlTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <Path Data="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=Data}"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Stroke="Black"/>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </ControlTemplate>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </Setter.Value>&nbsp; &nbsp; &nbsp; &nbsp; </Setter>&nbsp; &nbsp; </Style></Window.Resources>
随时随地看视频慕课网APP
我要回答