WPF在模板中绑定mahapps metro图标
我想将此按钮代码转换为模板版本,并将其放入样式xaml文件中:WPF在模板中绑定mahapps metro图标,wpf,templates,binding,mahapps.metro,Wpf,Templates,Binding,Mahapps.metro,我想将此按钮代码转换为模板版本,并将其放入样式xaml文件中: <Button Background="Transparent" BorderBrush="Transparent" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="20" Foreground="White" Opacity="1"> <StackPanel Orientation="Horizontal">
<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>
在理想情况下,应在模板的VisualAttribute中动态设置VisualProperty中指定的图标
我想出了这个解决方案,但它不起作用。绑定只能在DependencyProperty上完成
知道怎么做吗?我对模板表达式不太在行:///p>您可以使用绑定到按钮标记属性的VisualBrush定义模板:
伟大的它起作用了!现在我要动态生成几个按钮:D谢谢!这引出了另一个问题:如何动态构建这种按钮?尺寸btn作为新按钮btn.Content=John Doe btn.Style=_stylebtn.Tag=New IconsPack With{.Width=48、.Height=48、.Kind=Face}?嗯,是的,我必须用VB编程:/不是最好的选择,但不是我的。如果你有其他问题,请问一个新问题。不要在评论部分提出其他问题。
<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>
<Button Style="{StaticResource IconStyle}" Foreground="Green">
<Button.Tag>
<iconPacks:PackIconMaterial Kind="Face" Width="24" Height="24" />
</Button.Tag>
</Button>