【问题标题】:WPF How to bind mahapps icon dynamic object to button template styleWPF如何将mahapps图标动态对象绑定到按钮模板样式
【发布时间】:2017-06-20 06:57:59
【问题描述】:

我想动态设置按钮的图标。当我使用 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 中建议了一个可以工作的静态版本)。 如何将这些动态对象绑定到样式表?我应该使用转换器吗?还是有其他更简单的方法?

谢谢。

【问题讨论】:

    标签: wpf vb.net xaml mahapps.metro


    【解决方案1】:

    如果稍微修改模板:

    <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
    

    【讨论】:

    • 你成就了我的一天!谢谢!
    • 不适合我。我从这篇文章中复制了样式和按钮,我得到的只是一个没有图标的“John Doe”按钮。将 iconPacks 绑定到 Tag 属性似乎不起作用。
    猜你喜欢
    • 1970-01-01
    • 2018-11-01
    • 2021-12-17
    • 2017-02-20
    • 1970-01-01
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多