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.
Related
I have a weird bug and i need some rescue.
I have a grid with several column of multiple types in WPF.
One or several of these columns are DatePickers that I created through a FrameElementFactory :
FrameworkElementFactory dateFactory = new FrameworkElementFactory(typeof(DatePicker));
...
column = new DataGridTemplateColumn { CellTemplate = new DataTemplate
{ VisualTree = dateFactory } };
this._mainDatagrid.Columns.Add(column);
I have put a method to disable the DatePickers of my grid on a certain state of one of my variable:
private IEnumerable<DataGridRow> GetDataGridRows(DataGrid grid)
{
//return the Datagrid Rows
}
public void SetChangeLockState(bool isUnlocked)
{
IEnumerable<DataGridRow> _rows = this.GetDataGridRows(this._mainDatagrid);
foreach (DataGridColumn _column in this._mainDatagrid.Columns)
{
if (_column.GetType() != typeof(DataGridTemplateColumn)) continue;
foreach (DataGridRow _row in _rows)
{
FrameworkElement frameworkElement = _column.GetCellContent(_row);
if (frameworkElement != null) frameworkElement.IsEnabled = !isUnlocked;
}
}
}
The problem is that when I am playing with the elevator of my grid, the Datepicker keep enabling and disabling for no reason.
Example:
All my DatePicker are enabled, I am playing with my vertical scroll bar, no problem.
All my DatePickers are Disabled, I am playing with my vertical scroll bar.
1 Datepicker will suddenly appear enable :
DatePicker enabled 1
I am keeping playing with the scrollbar and another Datepicker will go enabled :
DatePicker enabled 2
Have you any idea of what could happen ?
Thanks for your help.
This will be because DataGrid.EnableRowVirtualization defaults to true. This enables UI virtualisation, which means that UI Elements that are scrolled out of view may be disposed or reused. Thus on occasions when scrolling an item back into view a new DatePicker will be created via your factory, and of course this new DatePicker will not have existed when SetChangeLockState was called and thus will not be disabled.
A quick fix would be to set DataGrid.EnableRowVirtualization to false, but this may not be very performant if you have lots of rows. A better solution would be to bind rather than set the IsEnabled property, e.g. to a property on your Window using RelativeSource.
Thanks to Ben, here is the code :
dateFactory.SetBinding(
//My IsEnabled property I wanted to change
IsEnabledProperty,
new Binding("IsLockedDatagrid")
{
//Datagridwidget is the datagrid I am using where I can found the IsLockedDatagrid boolean variable (in my xaml)
RelativeSource =
new RelativeSource(RelativeSourceMode.FindAncestor, typeof(DataGridWidget), 1),
Mode = BindingMode.OneWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged
});
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.
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.
UPDATE:
I think i have found the problem.
All my forms events that affect the binding source have this in the end:
BndSource.ResetBindings(false);
If i comment this line in my CheckedChanged event handler, the issue stops. But why?
I have a very strange bug.
I have a class property:
public SqlByte AutomaticFlag { get; set; }
I wanted to use checkbox to facilitate for showing this so in initial inding i do this:
dtaAutomaticFlag.DataBindings.Add("Checked", BndSource, "AutomaticFlag", true);
dtaAutomaticFlag.DataBindings[0].Format += (s, e) =>
{
if ((SqlByte)e.Value == 1)
{
e.Value = true;
}
else
{
e.Value = false;
}
};
the problem is that during iteration through all records of the binding source my ui is half updated, meaning its not complete. See picture:
VERY strangely when i change the above binding property from checked to text like this:
dtaAutomaticFlag.DataBindings.Add("Text", BndSource, "AutomaticFlag", true);
the ui is ok!!
Picture:
I'm not sure if this applies to this particular situation. But rather than adding the binding as you did:
dtaAutomaticFlag.DataBindings.Add("Text", BndSource, "AutomaticFlag", true);
Does creating a "new" binding instance help at all?
dtaAutomaticFlag.DataBindings.Add(new Binding("Text", BndSource, "AutomaticFlag", true));
I'm new to wpf. With Windows Forms and a DataGridView I can force a user to stay in edit mode if the value entered in the grid is not valid (for example when entering letters in a column with ValueType set to double) with a code like this:
DataGridView grid = new DataGridView();
grid.DataError += (sender, e) => { };
I'm trying to have similar behavior with the wpf DataGrid with no luck.
I tried the following binding options:
dataGrid.Columns.Add(new System.Windows.Controls.DataGridTextColumn() { Header = "aHeader", Binding = new System.Windows.Data.Binding("aProperty") { ValidatesOnDataErrors = false, ValidatesOnExceptions = false } });
But I can't see any change at all. Entering a non valid data causes the app to crash.
Any idea please?