2016-01-13 29 views
0

我有一個窗口,我想從另一個有少數RadioButton的部分改變這個窗口的一部分。從這個單選按鈕中,我希望客戶端選擇哪個部分需要在窗口的另一部分中看到。像Visual Studio中的「選項」窗口一樣。如何使用MVVM模式獲得組中的選定單選按鈕?

我在一個組中有幾個單選按鈕,並希望視圖模型知道當用戶選擇其中一個單選按鈕時選擇哪個單選按鈕。

我找到了一個example how I can bind in XAML,但無法理解視圖模型如何知道選擇了哪個單選按鈕。

這裏是我的簡單看法:

<RadioButton GroupName="Settings" IsChecked="{Binding SelectSettingSection}" Content="Language" /> 
<RadioButton GroupName="Settings" IsChecked="{Binding SelectSettingSection}" Content="Color" /> 
<RadioButton GroupName="Settings" IsChecked="{Binding SelectSettingSection}" Content="Description" /> 
<RadioButton GroupName="Settings" IsChecked="{Binding SelectSettingSection}" Content="Theme" /> 
<RadioButton GroupName="Settings" IsChecked="{Binding SelectSettingSection}" Content="Database" /> 

可能只有一個屬性這組單選按鈕,或者我需要有五個屬性對視圖模型的每個單選按鈕?

「SelectSectionSetting」是知道誰部分被選擇,我知道被選中,但到底是誰我不知道

+1

您的codeproject鏈接是最佳解決方案,請花一些時間來理解它。 –

+0

包含'SelectSettingSection'的屬性定義 –

+0

您應該添加一個ViewModel。 http://stackoverflow.com/questions/4420911/get-selected-radio-button-in-a-group-wpf – lyz

回答

0

讓我來幫助您瞭解您發佈的CodeProject上的鏈接,Novitchi S說是一個會爲您指出正確的解決方案。

它真正的訣竅是轉換器,它有兩個值1.所選對象(在單選按鈕中有界限)2.與單選按鈕關聯的對象(作爲轉換器參數傳遞)。轉換器的作用是檢查與單選按鈕對象相關的是否與所選對象相同(等於)。然後它將此值轉換爲true或false,並將其傳遞給單選按鈕

相關問題