WPF如何将Mahapps图标动态对象绑定到按钮模板样式

3

我希望能动态设置按钮的图标。由于我使用mahapps的图标,代码大致如下:

<Button x:Name="btnTest" Style="{StaticResource AppButton}" Content="Website" Tag="{iconPacks:PackIconMaterial Kind=Web}"/>

而且样式/模板是:
<ControlTemplate TargetType="{x:Type Button}" >
    <StackPanel Name="ButtonGrid" RenderTransformOrigin="0.5,0.5" DataContext="{Binding RelativeSource={RelativeSource AncestorType=Button}}">
        <Rectangle Width="48" Height="48" Fill="{Binding Foreground}">
            <Rectangle.OpacityMask>
                <VisualBrush Stretch="Fill" Visual="{Binding Tag}" />
            </Rectangle.OpacityMask>
        </Rectangle>
        <TextBlock Text="{Binding Path=Content, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"/>
    </StackPanel>
</Control.Template>

我通过以下VB.NET代码构建图标:

Private Sub ShowAppsTest()

        Dim _style As Style = Me.FindResource("AppButton")

        Test.Children.Clear()
        For Each app As UserApplication In _user.applications

            Dim btn As New MyApplicationButton(app.ApplicationName, app.ApplicationPath, app.ApplicationArgs, app.ApplicationIcon)
            btn.Content = app.ApplicationName & "_test"
            btn.Style = _style
            Dim materialIcon As New PackIconSimpleIcons() With {.Kind = PackIconMaterialKind.Cube, .Width = 48, .Height = 48}
            btn.Tag = materialIcon

            AddHandler btn.Click, AddressOf launchApp

            Test.Children.Add(btn)

        Next

    End Sub

但是,按照这种方式使用标签并不起作用(@mm8在这篇帖子中建议使用一个工作的静态版本WPF binding mahapps metro icons in template)。我该如何将这些动态对象绑定到样式表中?我应该使用转换器吗?还是有其他更简单的方法?谢谢。
1个回答

2
如果您稍微修改模板:
<ControlTemplate TargetType="{x:Type Button}" >
    <StackPanel Name="ButtonGrid" RenderTransformOrigin="0.5,0.5" DataContext="{Binding RelativeSource={RelativeSource AncestorType=Button}}">
        <Rectangle Width="48" Height="48" Fill="{Binding Foreground}">
            <Rectangle.OpacityMask>
                <VisualBrush Stretch="Fill">
                    <VisualBrush.Visual>
                        <iconPacks:PackIconMaterial Kind="{Binding Tag, RelativeSource={RelativeSource AncestorType=Button}}" 
                                                    Width="24" Height="24" />
                    </VisualBrush.Visual>
                </VisualBrush>
            </Rectangle.OpacityMask>
        </Rectangle>
        <TextBlock Text="{Binding Path=Content, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"/>
    </StackPanel>
</ControlTemplate>

...你可以将 Tag 属性设置为一个枚举值:

<Button x:Name="btnTest" Style="{StaticResource AppButton}" Content="Website" Tag="{x:Static iconPacks:PackIconMaterialKind.Web}" />

Dim btn As New MyApplicationButton(app.ApplicationName, app.ApplicationPath, app.ApplicationArgs, app.ApplicationIcon)
...
btn.Tag = PackIconMaterialKind.Cube

1
你让我的一天变得美好!谢谢! - Dams
对我不起作用。我已经从这篇文章中复制了样式和按钮,但我得到的只是一个带有“John Doe”文本的按钮,没有图标。将iconPacks绑定到Tag属性似乎不起作用。 - Kappacake

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接