在wpf中为mahapps tile创建自定义控件

时间:2017-07-17 12:36:14

标签: c# wpf tile

我不想为mahapps创建一个自定义控件来制作一个自定义磁贴控件,它具有visual的绑定属性,可以在tile中创建一个图标

控制xaml代码

<Style TargetType="{x:Type local:iTile}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:iTile}">
                <controls:Tile Title="{TemplateBinding Title}"
                               HorizontalTitleAlignment="Left"
                               HorizontalContentAlignment="Right"
                               Height="100" Width="100"
                               Margin="0"
                               VerticalContentAlignment="Top">
                    <Rectangle Fill="Black" Height="45" Width="45" Margin="0,10,8,0">
                        <Rectangle.OpacityMask>
                            <VisualBrush Visual="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Icon}" Stretch="Uniform" />
                        </Rectangle.OpacityMask>
                    </Rectangle>
                </controls:Tile>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

控制csharp代码

public class iTile : Tile
{
    static iTile()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(iTile), new FrameworkPropertyMetadata(typeof(iTile)));
    }

    public static readonly DependencyProperty IconProperty =
    DependencyProperty.Register("Icon", typeof(Object), typeof(iTile), new UIPropertyMetadata());

    public Object Icon
    {
        get { return GetValue(IconProperty); }
        set { SetValue(IconProperty, value); }
    }
}

并像这样控制使用

<iControls:iTile Title="Some Title"
                         Icon="{iconPacks:PackIconMaterial Kind=AccountOff}"/>

但图标不仅仅显示了图块的标题。

1 个答案:

答案 0 :(得分:0)

您可以在模板中将Visual属性设置为PackIconMaterial

<ControlTemplate TargetType="{x:Type local:iTile}">
    <controls:Tile Title="{TemplateBinding Title}"
                               HorizontalTitleAlignment="Left"
                               HorizontalContentAlignment="Right"
                               Height="100" Width="100"
                               Margin="0"
                               VerticalContentAlignment="Top">
        <Rectangle Fill="Black" Height="45" Width="45" Margin="0,10,8,0">
            <Rectangle.OpacityMask>
                <VisualBrush Stretch="Uniform">
                    <VisualBrush.Visual>
                        <iconPacks:PackIconMaterial Kind="{Binding Icon, RelativeSource={RelativeSource TemplatedParent}}" />
                    </VisualBrush.Visual>
                </VisualBrush>
            </Rectangle.OpacityMask>
        </Rectangle>
    </controls:Tile>
</ControlTemplate>

...然后将Icon属性设置为枚举值:

<iControls:iTile Title="Some Title"
             Icon="{x:Static iconPacks:PackIconMaterialKind.AccountOff}"/>