【问题标题】:WPF binding mahapps metro icons in template模板中的 WPF 绑定 mahapps 地铁图标
【发布时间】:2017-06-13 12:31:31
【问题描述】:

我想将此按钮代码转换为模板版本,并将其放入样式 xaml 文件中:

<Button Background="Transparent" BorderBrush="Transparent" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="20" Foreground="White" Opacity="1">
        <StackPanel Orientation="Horizontal">
            <Rectangle Width="24" Height="24" Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}">
                <Rectangle.OpacityMask>
                    <VisualBrush Stretch="Fill" Visual="{iconPacks:PackIconMaterial Kind=Face, Width=24, Height=24}" />
                </Rectangle.OpacityMask>
            </Rectangle>
            <TextBlock Margin="10 0 10 0" VerticalAlignment="Center" Text="John Doe" FontSize="24" FontWeight="Normal" FontFamily="Segoe UI Light" />
        </StackPanel>
    </Button>

我到现在都有这个:

<Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">

                <StackPanel Name="ButtonGrid" RenderTransformOrigin="0.5,0.5">
                    <Rectangle Width="48" Height="48" Fill="{Binding RelativeSource={RelativeSource AncestorType=Button}, Path=Foreground}">
                        <Rectangle.OpacityMask>
                            <VisualBrush Stretch="Fill" Visual="{Binding Path=(extensions:Icons.IconKind), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"/>
                        </Rectangle.OpacityMask>
                    </Rectangle>
                    <iconPacks:PackIconSimpleIcons x:Name="btnIcon" Kind="{Binding Path=(**extensions:Icons.IconKind**), RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}"  Width="48" Height="48" VerticalAlignment="Top" HorizontalAlignment="Center" Foreground="{TemplateBinding Foreground}" />
                    <TextBlock x:Name="tButton" HorizontalAlignment="Center" VerticalAlignment="Bottom" Foreground="{TemplateBinding Foreground}" FontWeight="Bold" Text="{Binding Path=Content, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" />

                    <StackPanel.RenderTransform>
                        <ScaleTransform ScaleX="1.0" ScaleY="1.0" CenterX="0.5" CenterY="0.5"/>
                    </StackPanel.RenderTransform>
                </StackPanel>

                <ControlTemplate.Triggers>
                    ...
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>

理想情况下,视觉属性中指定的图标应该在模板的视觉属性中动态设置。

我想出了这个解决方案,但它不起作用。绑定只能在 DependencyProperty 上完成。

&lt;VisualBrush Stretch="Fill" Visual="{iconPacks:PackIconMaterial Kind={Binding Path=(extensions:Icons.IconKind)}, Width=48, Height=48}"/&gt;

知道怎么做吗?我不太擅长模板表达式:/

【问题讨论】:

    标签: wpf templates binding mahapps.metro


    【解决方案1】:

    您可以使用绑定到ButtonTag 属性的VisualBrush 来定义Template

    <Style x:Key="IconStyle" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <StackPanel Orientation="Horizontal"
                                DataContext="{Binding RelativeSource={RelativeSource AncestorType=Button}}">
                        <Rectangle Width="24" Height="24" Fill="{Binding Foreground}">
                            <Rectangle.OpacityMask>
                                <VisualBrush Stretch="Fill" Visual="{Binding Tag}" />
                            </Rectangle.OpacityMask>
                        </Rectangle>
                        <TextBlock Margin="10 0 10 0" VerticalAlignment="Center" Text="John Doe" 
                                   FontSize="24" FontWeight="Normal" FontFamily="Segoe UI Light" />
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    

    ...然后将Tag 属性设置为一个图标:

    <Button Style="{StaticResource IconStyle}" Foreground="Green">
        <Button.Tag>
            <iconPacks:PackIconMaterial Kind="Face" Width="24" Height="24" />
        </Button.Tag>
    </Button>
    

    【讨论】:

    • 太棒了!有用!现在我必须动态生成几个按钮:D 谢谢!
    • 这就引出了另一个问题:如何动态构建这种按钮? Dim btn As New Button() btn.Content = "John Doe" btn.Style = _style btn.Tag = New IconsPack() With {.Width = 48, .Height = 48, .Kind = "Face"} ? (嗯,是的,我必须用 VB 编写代码:/ 不是最好的选择,但不是我的)
    • 如果您还有其他问题,请提出新问题。不要在 cmets 部分提出其他问题。
    • 不适合我。我已经从这篇文章中复制了样式和按钮,我得到的只是一个没有图标的“John Doe”按钮。标签绑定似乎不起作用。
    猜你喜欢
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-25
    相关资源
    最近更新 更多