我需要一個可以接受多個參數的ToggleButton命令處理程序,即所述ToggleButton的IsChecked屬性以及常量值,該常量值可以是字符串,字節, int ...無關緊要。需要使用MultiBinding綁定多個CommandParameters的額外幫助
我找到this great question on SO並按照答案的鏈接閱讀了MultiBinding和IMultiValueConverter。它真的去了,直到順利,我不得不寫MultiBinding,當我意識到我還需要通過一個恆定值,不能做類似
<Binding Value="1" />
我然後在另一個similar question that Kent Boogaart answered來了,然後我就開始想想我能解決這個問題的方法。
一種可能的方式是不使用MVVM和簡單的標籤屬性添加到我的切換按鈕,在這種情況下,我MultiBinding應該是這樣的:
<MultiBinding Converter="{StaticResource MyConverter}">
<MultiBinding.Bindings>
<Binding Path="IsChecked" />
<Binding Path="Tag" />
</MultiBinding.Bindings>
</MultiBinding>
肯特作出了沿線的評論,「如果你使用MVVM,你應該能夠解決這個問題「。不過,我不確定這對我來說是一種選擇,儘管我已經採用MVVM作爲我的WPF模式
必然
的選擇。
我之所以這樣說,是因爲我在wayControl中有多於一個ToggleButton,並且每個ToggleButton的命令都需要調用相同的函數。但由於它們是ToggleButtons,因此我不能使用ViewModel中綁定到IsChecked的屬性,因爲我不知道最後一次點擊了哪一個。我想我可以添加另一個私有財產來跟蹤這一點,但它似乎有點愚蠢。就常數而言,如果我做了追蹤的想法,我可能會擺脫這一點,但不知道有什麼其他方式來解決它。
有沒有人對我有好的建議? :)
編輯 - 好了,我需要更新我的綁定,它仍然不工作很正確:
<ToggleButton Tag="1" Command="{Binding MyCommand}" Style="{StaticResource PassFailToggleButtonStyle}" HorizontalContentAlignment="Center" Background="Transparent" BorderBrush="Transparent" BorderThickness="0">
<ToggleButton.CommandParameter>
<MultiBinding Converter="{StaticResource MyConverter}">
<MultiBinding.Bindings>
<Binding Path="IsChecked" RelativeSource="{RelativeSource Mode=Self}" />
<Binding Path="Tag" RelativeSource="{RelativeSource Mode=Self}" />
</MultiBinding.Bindings>
</MultiBinding>
</ToggleButton.CommandParameter>
</ToggleButton>
被器isChecked工作,而不是標籤。我只是意識到,標籤是一個字符串...呃。它正在工作!關鍵是使用Self的RelativeSource。
我上面的綁定是錯誤的,因爲它正在查詢虛擬機。任何人都有提示,或者你能推薦一個好的在線「備忘單」來找出數據綁定路徑嗎?我通常會在輸出窗口中查看錯誤,看看它與哪些內容綁定。現在,我想我必須設置RelativeSource屬性,但之前沒有使用它。 – Dave 2010-01-29 21:53:04
好吧,我發現這個:http://stackoverflow.com/questions/84278/how-do-i-use-wpf-bindings-with-relativesource – Dave 2010-01-29 21:53:42
綁定到一個常量的可能技巧:使用資源。在你的參考資料部分中, 1',然後'{Binding Source =「{StaticResource One}」}'。完全沒有經過測試。 –
itowlson
2010-01-30 00:16:32