1
我想動態設置按鈕的圖標。當我使用mahapps圖標我有這樣的事情:WPF如何將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)。 如何將這些動態對象綁定到樣式表?我應該使用轉換器嗎?還是有另一種更簡單的方法?
謝謝。
你讓我很快樂!謝謝 ! – Dams