簡體   English   中英

將未綁定的對象作為CommandParameter傳遞

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM