Bound UserControl is not updating data source - c#

I have a UserControl that has a Textbox, Button, and a Tooltip controls on it. It does implement INotifyPropertyChanged I have tried overriding the Text property and adding my own property, but in all cases the control reads from the bound data source fine, but never updates the data source. My events are raised when the text is changed. Some of the code is below. All other standard controls are working fine. What do I need to get the control to update the data source when the user has entered or changed the value?
public partial class UrlControl : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
[Bindable(true)]
[Browsable(true)]
public string Url
{
get
{
return url.Text;
}
set
{
if (value != url.Text)
{
url.Text = value;
OnPropertyChanged("Url");
}
}
}
protected virtual void OnPropertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
{
PropertyChanged(this, e);
}
}
This is the binding code from the form designer.
this.urlControl1.DataBindings.Add(new System.Windows.Forms.Binding("Url", this.customerBindingSource, "First", true, System.Windows.Forms.DataSourceUpdateMode.OnPropertyChanged));
}

INotifyPropertyChanged is for datasources. It allows your datasource to notify bound controls and other listeners of property changes. However, controls themselves use a different mechanism. It's a bit strange: you create events on your control with the naming convention <PropertyName>Changed. When the value of a property changes, you raise the associated event.
Example:
public string Url
{
get { return url.Text; }
set
{
if (value != url.Text)
{
url.Text = value;
OnUrlChanged(); // raise event
}
}
}
public event EventHandler UrlChanged;
private void OnUrlChanged()
{
// raise the UrlChanged event
if (UrlChanged != null)
UrlChanged(this, new EventArgs());
}
That's all you need to do. The Databinding Fairies will see that event and hook it up when you create the binding.
Here's the topic on MSDN: How to: Apply the PropertyNameChanged Pattern

This should work well for reading values from the datasource.
However, when it comes to writing values to the datasource it looks like you're storing and getting the Url value directly from the url textbox. However, you're not raising property change notifications when the textbox's text is changed within the UI. To fix this, add a TextChanged event handler on the textbox, which can simple call:
void url_TextChanged(object sender, EventArgs e)
{
OnPropertyChanged("Url");
OnUrlChanged(); // See additional note below
}
As a side, although implementing INotifyPropertyChanged should work... When it comes to Windows Forms binding you can also create an event with the property name suffixed with "Changed" and the binding should watch that:
public event EventHandler UrlChanged;
protected virtual void OnUrlChanged()
{
var handler = UrlChanged;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
[Bindable(true)]
[Browsable(true)]
public string Url
{
get
{
return url.Text;
}
set
{
if (value != url.Text)
{
url.Text = value;
OnPropertyChanged("Url");
OnUrlChanged();
}
}
}

Related

Control bound to property value doesn't update

I'm having a bit of a problem with WPF property binding. First the code.
C#
public partial class WPFTextBox: UserControl
{
private bool _bold;
public bool Bold
{
get { return _bold; }
set
{
_bold = value;
OnPropertyChanged("Bold");
}
}
private bool _selectionChanged;
public WPFTextBox()
{
InitializeComponent();
DataContext = this;
Bold = true; // <--- This works, the checkbox will be checked
_selectionChanged = false;
}
private void txtDetails_SelectionChanged(object sender, RoutedEventArgs e)
{
var selection = txtDetails.Selection;
_selectionChanged = true;
Bold = selection.FontWeight() == FontWeights.Bold;
// ^-- This doesn't work It will trigger everything, but the checkbox won't
// change value. FontWeight() is an extension I wrote
_selectionChanged = false;
}
private void OnPropertyChanged(string name)
{
if(_selectionChanged)
return; // If the change was brought from the user moving the
// cursor in the textbox, don't change the textbox.
TextRange range = txtDetails.Selection;
switch(name)
{
case "Bold":
// change selection to bold, like I mentioned I does work
break;
default:
break;
}
}
}
XAML
<RichTextBox Name="txtDetails" SelectionChanged="txtDetails_SelectionChanged"/>
<CheckBox Name="chkBold" Content="Bold" IsChecked="{Binding Path=Bold}"/>
I'm creating a textbox with format options. The binding works in the constructor, but not in the selection changed event. I've tried adding a lot of options to the binding such as Mode=TwoWay and different property changed triggers.
The reason I'm using the _selectionChanged bool is because if I don't check for that, if I have a word was different formatting such as hello and I click on it, it will change the formatting for all of the word to either bold or not. I think maybe it's because I'm handling it in selection changed event, but then I'm not sure where else I could change property value.
See the example from here, you can just grab the INPC part.
set
{
_bold = value;
OnPropertyChanged("Bold");
NotifyPropertyChanged();
}
You need to inherit INotifyPropertyChanged interface
and implement PropertyChangedEventHandler
public class WPFTextBox: UserControl,System.ComponentModel.INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
And call OnPropertyChanged in the setter of your property
1.You can use UpdateSourceTrigger=PropertyChanged event also.
2.Yes binding work on controls from the Extended WPF Toolkit.
IsChecked="{xcd:Path=Bold}"

DataBinding Radio Buttons

I will try to keep this concise as possible.
I am having an issue where I have a two way databinding with 3 radio buttons. My issue is, I am getting sort of a cyclical change as soon as I change the chosen radio button. So what is happening, I change the radio button which changes the source data property, but the source data property changes the other 2 properties that are bound, which in turn changes the other radio buttons, which calls the PropertyChange function on those properties as well. How can I fix it so that the PropertyChange only happens once per radio button switch.
Property Changed Event Handler:
public class SolutionOptions : INotifyPropertyChanged
{
bool _direct;
bool _iterative;
bool _domain;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
public bool Domain
{
get { return _domain; }
set
{
if (_domain == value) return;
_domain = value;
OnPropertyChanged("Domain");
}
}
public bool Iterative
{
get { return _iterative; }
set
{
if (_iterative == value) return;
_iterative = value;
OnPropertyChanged("Iterative");
}
}
public bool Direct
{
get { return _direct; }
set
{
if (_direct == value) return;
_direct = value;
OnPropertyChanged("Direct");
}
}
Data Binding Code:
this.radioButton_DirectSolver.DataBindings.Add("Checked", _ssf, "SolOptions.Direct");
this.radioButton_IterSolver.DataBindings.Add("Checked", _ssf, "SolOptions.Iterative");
this.radioButton_DomainDecomp.DataBindings.Add("Checked", _ssf, "SolOptions.Domain");
Form Image:
First try adding this to all your setters
if (_domain == value) return;
So you don't call OnPropertyChanged if it does not change.
This is a good practice in general.
Second (only if the first does not work) put each button in a separate group so they don't call each other and handle setting others to false in your code behind.

c# DataChanged event does trigger on a windows form (Desktop Application)

I have a form, I select some checkboxes, edit some text field, select from a combobox etc. then I click Exit. Based on the fact that "Data has been changed ??" I wish to perform actions. The problem is I can't get the event work :
private void DataChanged(object sender, EventArgs e)
{
MessageBox.Show("Data is changed", "debug");
isDataSaved = false;
}
When is this method called, how do I make it work? Is this supposed to get fired when the form's fields have some data i.e filL a text box ?
I dont really get the API: DataChanged event
Note: I'm following Mike Murach C# 5th edition chapter 10 example.
Edit (exact words from book):
Generate an event handler named DataChanged for the
SelectedIndexChanged event of the XXXX Name combo box. Then , wire
this event handler to the TextChanged event of the YYYYY Method label
and add the code to this event handler so it sets the isDataSaved
variable to false
When I double click on the commbo box the generated event handler it is not named DataChanged but cboNames_SelectedIndexChanged... (is this a book screw up or me total noob ? PS: There is no .. 'database' in the project)
Personally I mostly use databinding these days to get notified of changes in data.
A data holder class, which implements INotifyPropertyChanged. This interface gives you the possibility to get notified when the value of a property changes.
public class SomeData: INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
private void SetProperty<T>(ref T field, T value, [CallerMemberName] string name = "") {
if (!EqualityComparer<T>.Default.Equals(field, value)) {
field = value;
var handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(name));
}
}
}
private boolean _someBoolean;
public int SomeBoolean {
get { return _someBoolean; }
set {
SetProperty(ref _someBoolean, value);
}
}
private string _someString;
public string SomeString {
get { return _someString; }
set {
SetProperty(ref _someString, value);
}
}
// etc
}
Now our form, which uses the data class and it's INotifyPropertyChanged implementation to get notified when a change in data occurs.
public partial class SomeForm: Form {
private SomeData _data;
private void LoadData() {
_data = new SomeData();
_data.PropertyChanged += Data_PropertyChanged;
}
private void SaveData() {
// TODO: Save data
}
private void AddDataBindings() {
checkbox1.DataBindings.Add("Checked", _data, "SomeBoolean");
textbox1.DataBindings.Add("Text", _data, "SomeString");
// add other
}
private void Data_PropertyChanged(object sender, PropertyChangedEventArgs e) {
// Here you can add actions that must be triggered when some data changes.
if (e.PropertyName == "SomeBoolean") {
// Do something when some-boolean property changes
}
// Set is-changed-boolean to true to 'remember' that something has changed.
_isChanged = true;
// Give message
MessageBox.Show(string.Format("Data changed, property {0}", e.PropertyName));
}
private bool _isChanged = false;
protected void Form_Closed(object sender, EventArgs e) {
// If data is changed, save it
if (_isChanged) {
SaveData();
}
}
}
Your problem is not known where the method DataChanged use and how. I have a suggestion for you that is use Focus Activated in properties.Add datachanged printing method Activated
good luck.
You must make properties this like

Two way databinding in winforms, Inotifypropertychanged implemented in base class

I use Winforms Databinding and I have derived classes, where the base class implements IPropertychanged :
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName) {
var handler = this.PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
Each propertysetter calls:
protected void SetField<T>(ref T field, T value, string propertyName) {
if (!EqualityComparer<T>.Default.Equals(field, value)) {
field = value;
IsDirty = true;
this.RaisePropertyChanged(propertyName);
}
}
A typical Propertysetter:
public String LocalizationItemId {
get {
return _localizationItemId;
}
set {
SetField(ref _localizationItemId, value, "LocalizationItemId");
}
}
The way a property is bound to a textbox
private DerivedEntity derivedEntity
TextBoxDerivedEntity.DataBindings.Add("Text", derivedEntity, "Probenname");
If I programmatically assign text to the textbox, the textbox does not show it. But I can manually edit the textbox.
I know it is too late to answer, but this problem can be solved, if you set event when your binding should change value, if you set it on property value change event your problem will be solved. You can do this by this way
textBox.DataBindings.Add("textBoxProperty", entity, "entityProperty", true, DataSourceUpdateMode.OnPropertyChanged);
Binding source is updated on TextBox Validated event. TextBox validated event is called when user edit TextBox and then changes focus to other control.
Since you're changing TextBox text programmatically TextBox doesn't know that text were changed and therefore validation is not called and binding is not updated, so you need to update binding manually.
Initialize binding:
var entity;
textBox.DataBindings.Add("textBoxProperty", entity, "entityProperty");
Change TextBox.Text:
textBox.Text = "SOME_VALUE";
Update binding manually:
textBox.DataBindings["textBoxProperty"].WriteValue();
Binding.WriteValue() reads value from control and updates entity accordingly.
You could read about WriteValue at MSDN.
The subscriber isn't initialized. i.e.
private DerivedEntity derivedEntity
TextBoxDerivedEntity.DataBindings.Add("Text", derivedEntity, "Probenname");
derivedEntity is null.
Initialize it and you'll be fine.
I implemented the "INotifyPropertyChanged", but raise the PropertyChanged event only when the new value is different from the old value:
public class ProfileModel : INotifyPropertyChanged
{
private Guid _iD;
private string _name;
public event PropertyChangedEventHandler PropertyChanged;
public Guid ID
{
get => _iD;
set
{
if (_iD != value)
{
_iD = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ID"));
}
}
}
public string Name
{
get => _name;
set
{
if (_name != value)
{
_name = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name"));
}
}
}
}
Now just bind to the controls:
txtProfileID.DataBindings.Clear();
txtProfileID.DataBindings.Add("Text", boundProfile, "ID", true, DataSourceUpdateMode.OnPropertyChanged);

Databinding a Custom Control

I have a Custom Control (Windows Form) that is a lookup text box. A property on the Control is Current Selection which is a Custom Object containing "Identifier", "Code" and "Description". This property is Databound using a BindingSource.
Displaying the information works great. On the other hand regardless of whether I set the Update to OnValidate or OnValueChange it never updates the BindingSource. Is there something I'm missing to get this to auto update?
private System.Windows.Forms.BindingSource buildPlanComponentDataBindingSource;
public void LoadBuildPlan(string itemNumber)
{
var buildPlanComponents = BuildPlan.LoadBuildPlanComponents(itemNumber, AutomaticPrice);
buildPlanComponentDataBindingSource.DataSource = buildPlanComponents;
AssemblyNumber = itemNumber;
}
[Bindable(true)]
[DefaultValue(null)]
public ILookupSelection CurrentSelection
{
get
{
if (currentSelection == null)
currentSelection = new LookupSelection {Code = txtLookup.Text};
return currentSelection;
}
set
{
if (value == null) return;
currentSelection = value;
SetText(currentSelection, DisplayText);
SetDescription(currentSelection, DisplayDescription);
}
}
Implementing INotifyPropertyChanged seems to be the solution!
#region IPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (null != PropertyChanged)
{
PropertyChanged(this, e);
}
}
#endregion
Displaying the information works
great. On the other hand regardless of
whether I set the Update to OnValidate
or OnValueChange it never updates the
BindingSource.
Looking at your code I'm actually not sure of this. In your set, you test for null and abandon; if the data actually contains null (which is what you're describing) your control will be out of synch. I wonder if perhaps that check is masking the underlying problem.
Maybe you need to cause the DataBinding to write its value for each control whose value you are setting this way?
Assuming one data binding for a textbox named txtMySetValue:
txtMySetValue.DataBindings[0].WriteValue();

Categories

Resources