WPF Radiobutton (two) (binding to boolean value)

前端 未结 8 1973
深忆病人
深忆病人 2020-12-24 10:51

I have a property of type boolean presented with checkbox.

I want to change that to two radiobuttons that bind on the same property presenting the value true/false.<

相关标签:
8条回答
  • 2020-12-24 11:45

    When using MVVMLight and DataContext is set in XAML as:

    DataContext="{Binding <Your ViewModel property name>, Source={StaticResource Locator}}" 
    

    BoolInverterConverter causes Stack Overflow second time the window gets opened.

    The work around is to remove DataContext from XAML and do it in code in window constructor after InitializeComponent():

    DataContext = ServiceLocator.Current.GetInstance<Your View Model class>();
    

    After some testing it was not enough - Stack Overflow error could pop up randomly when clicking on radio button. The solution which worked for me - instead of the converter use another property for other radio button in a group:

    public bool Is9to1 { get; set; }
    public bool Is1to9 { get { return !Is9to1; } set { Is9to1 = !value; } } 
    

    in XAML:

       <RadioButton GroupName="Is9to1" IsChecked="{Binding Is1to9}"/>
       <RadioButton GroupName="Is9to1" IsChecked="{Binding Is9to1}"/>
    
    0 讨论(0)
  • 2020-12-24 11:47

    Here is the solution on how to bind radio buttons to any type (enumeration, Boolean, string, integer, etc.) with the sample code:

    http://www.codeproject.com/Tips/720497/Binding-Radio-Buttons-to-a-Single-Property

    0 讨论(0)
提交回复
热议问题