WPF Radiobutton (two) (binding to boolean value)

前端 未结 8 2042
深忆病人
深忆病人 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 , 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();
    

    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:

       
       
    

提交回复
热议问题