wpf - C# MVVM: Binding a RadioButton to a boolean Property -
i quiet new programming , learning c# , mvvm pattern.
i need code database tool chiliplants university. there should able add new object observablecollection.
to add new item observablecollection new window opens. looks this: window add
i want 2 radioboxes bound property called "hybridseed". defined in viewmodel:
//public property hybridseed public bool hybridseed { { return chilimodel.hybridseed; } set { if (chilimodel.hybridseed == value) return; chilimodel.hybridseed = value; onpropertychanged("hybridseed"); } }
the radiobox part of view looks this:
<radiobutton grid.row="5" content="ja" grid.column="1" horizontalalignment="left" margin="10,10,0,0" verticalalignment="top"/> <radiobutton grid.row="5" content="nein" grid.column="1" horizontalalignment="left" margin="89,10,0,0" verticalalignment="top"/>
but how bind outcome of user clicking on these radiobuttons hybridseed property? important outcome bool.
i looked every entry similar topic, did not find simple solution. or solution able understand bad coding skills :( ...
i happy if guys me. please keep simple newbie :)
if there simpler solution using checkbox or combobox perfect. important thing have nice user interface. right works textbox user has write "true" or "false".
solution:
i added isclicked property in "yes" radiobutton bound boulean property with: isclicked="{binding hybridseed}". naslund fast answer :)
just bind hybridseed yes-radiobutton. either true if user has selected or false if no-radiobutton has been selected (or if nothing has been selected). binding both buttons in case bit redundant since mechanism of radiobuttons takes care of it.
wpf:
<radiobutton content="yes" ischecked="{binding hybridseed}" /> <radiobutton content="no" /> <label content="{binding hybridseed}" contentstringformat="value is: {0}" />
logic:
public partial class mainwindow : window { public mainwindow() { initializecomponent(); datacontext = new viewmodel(); } } public class viewmodel : inotifypropertychanged { private bool hybridseed; public bool hybridseed { { return hybridseed; } set { hybridseed = value; onpropertychanged(nameof(hybridseed)); } } public event propertychangedeventhandler propertychanged; [notifypropertychangedinvocator] protected virtual void onpropertychanged([callermembername] string propertyname = null) { propertychanged?.invoke(this, new propertychangedeventargs(propertyname)); } }
Comments
Post a Comment