2013-09-04 8 views
11

के बाद WPF में समूहबॉक्स से चेक किए गए रेडियोबूटन को निर्धारित करना मेरे पास कुछ रेडियोबूटन के साथ एक समूह है। मुझे कैसे पता चलेगा कि कौन सा चेक किया गया है? मैं डब्ल्यूपीएफ और एमवीवीएम का उपयोग कर रहा हूं।एमवीवीएम

<GroupBox Name="grpbx_pagerange" Header="Print Range"> 
    <Grid > 
     <RadioButton Name="radbtn_all" Content="All Pages" GroupName="radios_page_range" IsChecked="True" /> 
     <RadioButton x:Name="radbtn_curr" Content="Current Page" GroupName="radios_page_range" /> 
     <RadioButton Name="radbtn_pages" Content="Page Range" GroupName="radios_page_range" /> 

     .... 

</GroupBox> 

अब, एक तरह से मैं यह पता लगाने सकता है ViewModel में कुछ संपत्ति के लिए प्रत्येक RadioButton के IsChecked संपत्ति बाध्य करने के लिए और फिर मेरी ViewModel में तर्क के if..else तरह कर चयनित radiobutton यह पता लगाने की थी।

लेकिन क्या कोई अन्य सुरुचिपूर्ण तरीका है?

उत्तर

27

आप अपने ViewModel के कमांड पर RadioButton.Command रेडियोबूटन से बाध्य कर सकते हैं और एक कमांड पैरामीटर भेज सकते हैं यह पहचानने के लिए कि किस बटन ने कमांडरलर में कमांड कहा है।

<RadioButton Command="{Binding MyCommand}" CommandParameter="Radio1"/> 
<RadioButton Command="{Binding MyCommand}" CommandParameter="Radio2"/> 
<RadioButton Command="{Binding MyCommand}" CommandParameter="Radio3"/> 

कमांड हैंडलर में रेडियोबूटन की पहचान करने के लिए पैरामीटर के लिए जांच करें।

धन्यवाद

+0

हाय @ नोट: यह बहुत ही सरल, त्वरित, सुरुचिपूर्ण और वास्तव में लागू समाधान के लायक है। आपके उत्तर के लिए धन्यवाद। :) – Bhramar

+0

@ भामर खुश है कि यह आपके लिए काम करता है :) – Nitin

+1

बडी, समाधान ठीक काम करता है, लेकिन मैं अपने किसी भी रेडियोबटन पर डिफ़ॉल्ट रूप से कैसे चेक करूंगा। ठीक है हम इसे निर्माता में सेट कर सकते हैं, लेकिन यह एमवीवीएम नियमों को कम करेगा। कोई भी विचार मददगार होगा । – Bhramar

18

आप एक enum कि नाम (मोटे तौर पर) के रूप में RadioButton वस्तुओं के मूल्यों को बना सकते हैं और फिर इस enum के प्रकार के एक EnumToBoolConverter उपयोग करने का एक संपत्ति के लिए IsChecked संपत्ति बाँध।

public enum Options 
{ 
    All, Current, Range 
} 
फिर अपने दृश्य मॉडल या पीछे कोड में

: उचित ConverterParameter की स्थापना UI में फिर अंत में

[ValueConversion(typeof(Enum), typeof(bool))] 
public class EnumToBoolConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (value == null || parameter == null) return false; 
     string enumValue = value.ToString(); 
     string targetValue = parameter.ToString(); 
     bool outputValue = enumValue.Equals(targetValue, StringComparison.InvariantCultureIgnoreCase); 
     return outputValue; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (value == null || parameter == null) return null; 
     bool useValue = (bool)value; 
     string targetValue = parameter.ToString(); 
     if (useValue) return Enum.Parse(targetType, targetValue); 
     return null; 
    } 
} 

, बाइंडिंग जोड़ने के लिए,:

private Options options = Options.All; // set your default value here 

public Options Options 
{ 
    get { return options; } 
    set { options = value; NotifyPropertyChanged("Options"); } 
} 

Converter जोड़े

<RadioButton Content="All Pages" IsChecked="{Binding Options, Converter={ 
    StaticResource EnumToBoolConverter}, ConverterParameter=All}" /> 
<RadioButton Content="Current Page" IsChecked="{Binding Options, Converter={ 
    StaticResource EnumToBoolConverter}, ConverterParameter=Current}" /> 
<RadioButton Content="Page Range" IsChecked="{Binding Options, Converter={ 
    StaticResource EnumToBoolConverter}, ConverterParameter=Range}" /> 

अब आप बता सकते हैं कि Options वैरिएबल को आपके व्यू मॉडल या कोड में देखकर कौन सा सेट किया गया है। आप Options संपत्ति सेट करके चेक किए गए RadioButton को भी सेट करने में सक्षम होंगे।

+0

हे @ शेरिडन: आपका समाधान बहुत अच्छा लग रहा है, खासकर 'एनम' और 'कन्वर्टर' फीचर। लेकिन मैं विंडो में स्थैतिक संसाधन आवंटित नहीं कर पा रहा हूं। संसाधन - यह उपयोग योग्य होगा - यदि आप स्थिर संसाधन को परिभाषित करने के अपने दृष्टिकोण को जोड़ सकते हैं। धन्यवाद। :) – Bhramar

+0

@ भामर आप इस तरह कनवर्टर शामिल कर सकते हैं: पहले xml नाम की तरह xml नामस्थान शामिल करें: स्थानीय == "clr-namespace: wpfAplication1" i, e तब नेमस्पेस जिसमें कनवर्टर घोषित किया गया है। तो में Window.Rsources जोड़ने <स्थानीय: EnumToBoolConverter एक्स: मुख्य = "EnumToBoolConverter" /> – Nitin

+0

मैं कोशिश की है इस: '<विंडो एक्स: नाम =" win_printDialog " एक्स: कक्षा =" MNCore.Control.DocumentPrintDialog " ... xmlns: स्थानीय = "clr-नाम स्थान: MNCore.Control" शीर्षक = "मुद्रण संवाद"> <स्थानीय: EnumToBoolConverter एक्स: मुख्य = "EnumToBoolConverter" /> ' यह सिर्फ काम नहीं कर रहा है। मैंने इस [आलेख] को संदर्भित किया है (http://www.amazedsaint.com/2009/08/mvvm-binding-multiple-radio-buttons-to.html) त्रुटि कहती है- "EnumToBoolConverter" नामस्थान में मौजूद नहीं है "क्लियर-नेमस्पेस: एमएनसीओआर.कंट्रोल" – Bhramar

-4

मुझे एक ही समस्या मिली और मैंने रेडियोबूटन से "ग्रुपनाम" संपत्ति को हटाकर हल किया।

कृपया सभी रेडियो बटनों से "समूह नाम" संपत्ति को हटा दें। और जाँच

1

इस का उपयोग करते हुए IsChecked संपत्ति

हल करने के लिए एक और MVVM तरीका नहीं है यहाँ XAML है

<Page> 
<Page.Resources> 
<DataTemplate x:Key="ChoiceItemTemplate"> 
<RadioButton Content="{Binding individualRadioButtonText}" 
    IsTabStop="True" 
    GroupName="choice" 
    IsChecked="{Binding IsChecked, Mode=TwoWay}"/> 
</DataTemplate> 
</Page.Resources> 


<StackPanel> 
    <TextBlock Text="{Binding ChoiceQuestion}" /> 
<ItemsControl ItemsSource="{Binding ListOfAnswerOptions}" 
       ItemTemplate="{StaticResource ChoiceItemTemplate}" /> 
</StackPanel> 
</Page> 

आपका मॉडल इस

public class RadioButtonQuestion 
{ 
    public string ChoiceQuestion { get; set; } 
    public string answer { get; set; } 
    public List<AnswerOption> ListOfAnswerOptions { get; set; } 
} 

public class AnswerOption 
{ 
    public string individualRadioButtonText { get; set; } 
    public bool IsChecked { get; set; } 
} 
कुछ

ViewModel दिखेगा की तरह कुछ हो जाएगा इस तरह (चयन तर्क)

RadioButtonQuestion r = new RadioButtonQuestion(); 
var selectedElement = rbuttonQuestion.answerOptions.FirstOrDefault(c => c.IsChecked); 
r.answer = selectedElement.individualRadioButtonText; 

तो यदि आप इस व्यूमोडेल के दृश्य के डेटाैकेंटेक्स्ट को सेट करते हैं। आप इसे काम करने में सक्षम होना चाहिए।

उम्मीद है कि यह मदद करता है।

1

आप अपने XAML में जैसे विकल्प बटन (बूलियन, पूर्णांक, तार) पर टैग संपत्ति का उपयोग करते हैं

<StackPanel Orientation="Horizontal" Margin="10,10, 0, 0"> 
    <RadioButton Name="rP0" Content="Low " Tag="0" /> 
    <RadioButton Name="rP1" Content="Normal" Tag="1" IsChecked="True" /> 
    <RadioButton Name="rP2" Content="Medium" Tag="2" /> 
    <RadioButton Name="rP3" Content="High" Tag="3" /> 
</StackPanel> 

तो आप निम्नलिखित समारोह का उपयोग कर सकते चयनित मान (बटन)

int priority = SelectedRadioValue<int>(0, rP0, rP1, rP2, rP3); 

पाने के लिए जहां

public T SelectedRadioValue<T>(T defaultValue, params RadioButton[] buttons) 
{ 
    foreach (RadioButton button in buttons) 
    { 
     if (button.IsChecked == true) 
     { 
      if (button.Tag is string && typeof(T) != typeof(string)) 
      { 
       string value = (string) button.Tag; 
       return (T) Convert.ChangeType(value, typeof(T)); 
      } 

      return (T)button.Tag; 
     } 
    } 

    return defaultValue; 
}