[英]Pass unbound object as CommandParameter
在WPF中是否有一種簡單的方法可以將Color
類型的對象作為CommandParameter
傳遞?
我有一個應該更改的按鈕列表,例如我的應用程序的背景色。
這些按鈕如下所示:
<Button Command={Binding SetBackgroundColor} CommandParameter={Color.Red} Background="Red" />
<Button Command={Binding SetBackgroundColor} CommandParameter={Color.Green} Background="Green" />
<Button Command={Binding SetBackgroundColor} CommandParameter={Color.White} Background="White" />
<Button Command={Binding SetBackgroundColor} CommandParameter={???} Background="#FF00FF" />
然后單擊彩色按鈕,就應該更改顏色。
有任何想法,模式,最佳做法,方法或解決方法嗎?
你可以寫
<Button CommandParameter="{x:Static Colors.Red}" ... />
要么
<Button ...>
<Button.CommandParameter>
<Color>#FF00FF</Color>
</Button.CommandParameter>
</Button>
或使用顏色資源:
<Window.Resources>
<Color x:Key="myColor">#FF00FF</Color>
...
</Window.Resources>
...
<Button CommandParameter="{StaticResource myColor}" ... />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.