Programmatically create a checkbox binding for a WPF form - c#

I need to programmatically create a binding for a checkbox that resides in a WPF form. Because the checkbox is in a user control that gets added to the form multiple times, I'm unsure how to do this. I've created a binding for a DevExpress RichEdit control which worked and then modified that code for the checkbox, but it didn't work.
My code to return the binding is as follows:
private Binding SetIsCorrectBinding(int row)
{
Binding binding = new Binding("DataModel.DetailList[" + row + "].IsCorrect")
{
Path = new PropertyPath("DataModel.DetailList[" + row + "].IsCorrect"),
Mode = BindingMode.TwoWay
};
return binding;
}
The code to implement the binding is as follows:
Binding cbBind = SetIsCorrectBinding(row);
detailRow.IsCorrect_cb.SetBinding(CheckBox.ContentProperty, cbBind);
No matter what I try, the IsCorrect variable is always false.
Any help with this would be greatly appreciated.

Please try the next:
var xBinding = new Binding();
//a real instance of the object where the source property is defined
//it have to be the same instance which is defined in DataModel.DetailList
xBinding.Source = sourceInstance;
xBinding.Path = new PropertyPath("The_source_property_name");
xBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
xBinding.Mode = BindingMode.TwoWay;
//Use this instead the .SetBinding( , ) where the checkbox is the object to binded to
BindingOperations.SetBinding(checkbox, CheckBox.ContentProperty, xBinding);
Please look at the next solution here, I think it can supply an additional information for you.
I will glad to help if you will have the problem with code.
regards,

6 years late, but I had a similar issue. You need to bind to the Checkbox.IsCheckedProperty
detailRow.IsCorrect_cb.SetBinding(CheckBox.IsCheckedProperty, cbBind);
was the fix you needed.

Related

C# Data Binding via code behind doesn't work for Text property in TextBox

I have a textbox with xaml markup like this:
<TextBox x:Name="txtHN" Text="{Binding Path=AN}"/>
The above code works perfectly well. But when i change the data binding implementation from XAML to code-behind, It does not work anymore. The following code-behind does not work anymore:
Binding textHnBinding = new Binding();
textHnBinding.Path = new PropertyPath("AN");
txtHN.SetBinding(TextBox.TextProperty, textHnBinding);
I had set the textbox.datacontext to the same collectionviewsource but the code-behind version does not work anymore. I had really no idea what seems to be the culprit.
I use the following code for the CVS.source:
IEnumerable<decimal> ANListWard4 = (from s in context.IPDAN
where ward.Contains(s.CURRENTWARD)
select s.AN).Distinct().OrderBy(n => n);
List<IPDAN> Ward4AN = new List<IPDAN>();
foreach (decimal d in ANListWard4)
{
IPDAN ward4AN1 = new IPDAN();
ward4AN1.AN = d;
Ward4AN.Add(ward4AN1);
}
I set the CVS.Source to Ward4AN. There was no instance where the Ward4AN was null, or has no data.
Check whether the collectionViewSource is not null when you are doing the binding.If the object is null when you are adding the binding, the binding might not work.
Also check whether in loaded event it works or not.
Can you try :
Binding textHnBinding = new Binding("AN");
txtHN.SetBinding(TextBox.TextProperty, textHnBinding);
This is how I done all my binding so I think it should work.
EDIT :
Long time no uses binding so my apologies if I'm wrong again :
Binding textHnBinding = new Binding("AN");
FrameworkElementFactory textHN = new FrameworkElementFactory(typeof(TextBox));
txtHN.SetBinding(TextBox.TextProperty, textHnBinding);
Can you try instantiate your Control like this instead of in Xaml to check if it will work (as me) ? Thank you.

CheckBox.DataBindings.Add not working

I am trying to bind a checkbox contained within a winforms data repeater, however the checkbox itself it not ticking. When binding to a label it works
lbSchoolFri.DataBindings.Add("Text", bindingSource5, "SchoolName");
Checkbox (not working) -
cbSchoolFri.DataBindings.Add("Checked", bindingSource5, "SchoolContacted");
Any ideas why this is not working?
Thanks
If it is a bit (0 or 1), you have to add Format event handler for your Binding:
Binding bind = new Binding("Checked", bindingSource5, "SchoolContacted");
bind.Format += (s,e) => {
e.Value = (int)e.Value == 1;
};
cbSchoolFri.DataBindings.Add(bind);
This is a very basic task when you work with Binding.
Another possibility: You need to add "true" as a parameter to Binding; see here... look at the bottom of the "UPDATE Aug 18" code sample.

ToolStripComboBox.SelectedItem change does not propagate to binding source

My binding is set up as this:
_selectXAxisUnitViewModelBindingSource = new BindingSource();
_selectXAxisUnitViewModelBindingSource.DataSource = typeof(SelectXAxisUnitViewModel);
_selectedUnitComboBoxBindingSource = new BindingSource();
_selectedUnitComboBoxBindingSource.DataSource = _selectXAxisUnitViewModelBindingSource;
_selectedUnitComboBoxBindingSource.DataMember = "AvailableUnits";
_selectedUnitComboBox.ComboBox.DataSource = _selectedUnitComboBoxBindingSource;
_selectedUnitComboBox.ComboBox.DisplayMember = String.Empty;
_selectedUnitComboBox.ComboBox.ValueMember = String.Empty;
_selectedUnitComboBox.ComboBox.DataBindings.Add("SelectedItem",
_selectXAxisUnitViewModelBindingSource,
"SelectedUnit", true, DataSourceUpdateMode.OnPropertyChanged);
// this is a bug in the .Net framework: http://connect.microsoft.com/VisualStudio/feedback/details/473777/toolstripcombobox-nested-on-toolstripdropdownbutton-not-getting-bindingcontext
_selectedUnitComboBox.ComboBox.BindingContext = this.BindingContext;
The property "AvailableUnits" is a collection of strings and the "SelectedUnit" is a string-property. Now the dropdown list is populated as expected, but when I select and item in the list, the change is not propagated to the binding source. Any idea what I am doing wrong?
Update:
I Created a little test project and this problem occurs when I add a ToolStripComboBox as a subitem of another ToolStripItem. If I add the ToolStripItem directly to the MenuStrip everything works fine. The BindingContext is not assigned to the ToolStripComboBox when added as a sub item (see my code-comment) and my fix doesn't seem to do whats necessary to get this to work.
Can you change
_selectXAxisUnitViewModelBindingSource.DataSource = typeof(SelectXAxisUnitViewModel);
To
_selectXAxisUnitViewModelBindingSource.DataSource = new SelectXAxisUnitViewModel();

What is the correct way to bind a textbox to an object

Could somebody please take some time to show me a quick example on how to bind a text box to the property of an object from c# code? (I've tried to do it on my own, but i can't seem to get it right.)
Thank you guys. I just spent an hour before i realized how stupid i am ( i was setting the wrong object as the biding source).
Thank you all for your help.
Binding b = new Binding();
b.Source = yourObject;
b.Path = new PropertyPath("YourProperty");
yourTextBox.SetBinding( TextBox.TextProperty, b );
There are a lot of other properties on binding you can set. The above does one-way binding, but you can change that by setting the Mode property.
Binding the TexeBox Text property with the "Name" property in the ViewModel
Binding binding = new Binding("Name");
binding.Source = ViewModel;
binding.Mode = BindingMode.TwoWay;
SomeTextBox.SetBinding(TextBox.TextProperty, binding);

setting control property in cs file

I want to set the control binding property "updatesource=Explicit" in cs file (dynamically) not in UI end. Please help me how can i do this?
it works :)
this.GetBindingExpression(SomeProperty).ParentBinding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
Are you creating the binding in code manually? If so you can just set it like any other property:
var binding = new Binding("BindingPath")
{
Source = myDataObject,
UpdateSourceTrigger = UpdateSourceTrigger.Explicit
}
textBlock.SetBinding(TextBlock.TextProperty, binding);
More information here.
I tested it and it works. :-) The code remains the same as Gimalay's.
BindingExpression bindingExpr = this.textBox1.GetBindingExpression(TextBox.TextProperty);
bindingExpr.ParentBinding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;

Categories