Designer auto-generating unwanted code I can't get rid of - c#

I just created a control with the following code:
public partial class KindsEditor : NaviGroupList, INotifyPropertyChanged
{
private WebBrowser _Browser;
private BasicProject _Project;
public event PropertyChangedEventHandler PropertyChanged;
public bool RequiredDataLoaded { get { return (Project != null) && (Browser != null); } }
private bool _ButtonsEnabled = false;
public bool ButtonsEnabled { set { SetButtonsEnabled(value); } get { return _ButtonsEnabled; } }
public WebBrowser Browser
{
get { return _Browser; }
set
{
_Browser = value;
OnPropertyChanged(new PropertyChangedEventArgs("Browser"));
OnPropertyChanged(new PropertyChangedEventArgs("RequiredDataLoaded"));
}
}
public BasicProject Project
{
get { return null; }
set { LoadProject(value); }
}
public KindsEditor()
{
InitializeComponent();
DataBindings.Add("ButtonsEnabled", this, "RequiredDataLoaded");
}
private void SetButtonsEnabled(bool value)
{
newKindButton.Enabled = value;
_ButtonsEnabled = value;
OnPropertyChanged(new PropertyChangedEventArgs("ButtonsEnabled"));
}
protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null) PropertyChanged(this, e);
}
private void LoadProject(BasicProject value)
{
if (value != null) DataSource = value.Kinds;
_Project = value;
OnPropertyChanged(new PropertyChangedEventArgs("Project"));
OnPropertyChanged(new PropertyChangedEventArgs("RequiredDataLoaded"));
}
}
I removed some stuff that I think is irrelevant to my problem. I was trying to bind one button (newKindButton) being enabled to two properties (Browser and Project) being not null. I know it's messy and no one would expect that I call OnPropertyChanged while changing a different property and other stuff that might not be supposed to be done. I'll fix that later. But the weird thing is that the Form that uses this control (I drag and dropped it from the toolbox) added this line to the InitializeComponent() auto-generated code:
this.kindsEditor1.DataBindings.Add(new System.Windows.Forms.Binding("ButtonsEnabled", this.kindsEditor1, "RequiredDataLoaded", true));
So when I try to run the app I get an exception telling me that this line is trying to bind to the same property twice. I swear, I never added any binding from the properties panel. If I remove the line
DataBindings.Add("ButtonsEnabled", this, "RequiredDataLoaded");
from KindsEditor's constructor, the auto-generated line disappears. Anyone knows what's going on?

Try adding DesignerProperties.GetIsInDesignMode around the binding:
public KindsEditor()
{
InitializeComponent();
if (!DesignerProperties.GetIsInDesignMode(this))
DataBindings.Add("ButtonsEnabled", this, "RequiredDataLoaded");
}
I don't have a direct answer, but I suspect that Visual Studio thinks it needs to serialize something (the generated code) when it shouldn't. The above construct hides the binding from Visual Studio and only activates it during runtime.

Related

How to update a property value when aother property value is changed C# WPF [duplicate]

Maybe here is already such question, but I didn't find it.
I have MVVM application, and in my ViewModel I have to do some additional actions on changes of some properties (for example, if View changes them). Which approach is better on your mind and why?
1st - Add AdditionalAction call to setter
public class ViewModel: INotifyPropertyChanged
{
private int _MyProperty;
public int MyProperty
{
get { return _MyProperty; }
set
{
if (_MyProperty == value) return;
_MyProperty = value;
RaisePropertyChanged(() => MyProperty);
// --- ADDITIONAL CODE ---
AdditionalAction();
}
}
}
2nd - Self-subscribe to INotifyPropertyChanged
public class ViewModel: INotifyPropertyChanged
{
public ViewModel()
{
// --- ADDITIONAL CODE ---
PropertyChanged += OnPropertyChanged;
}
private int _MyProperty;
public int MyProperty
{
get { return _MyProperty; }
set
{
if (_MyProperty == value) return;
_MyProperty = value;
RaisePropertyChanged(() => MyProperty);
}
}
void PropertyChanged(object sender, PropertyChangedEventArgs e)
{
// --- ADDITIONAL CODE ---
if (e.PropertyName == "MyProperty")
AdditionalAction();
}
}
Imagine, that I don't have performance problem or 10'000 objects. It's just View and ViewModel. What is better? First code is "smaller" and has less overhead, but the second (on my mind) is more clear and I can use code snippets for auto-generation properties' code. Even more - in the 2nd case I can write in event handler something like:
On.PropertyChanged(e, p => p.MyProperty, AdditionalAction);
where On is class-helper.
So, what is better on your mind and why?
UPDATED:
OK, it looks like I found yet one approach:
3rd - add "extension point" in RaisePropertyChanged:
public class NotificationObject : INotifyPropertyChanged
{
void RaisePropertyChanged(Expression<...> property)
{
// ... Raise PropertyChanged event
if (PropertyChanged != null)
// blah-blah
// Call extension point
OnPropertyChanged(property.Name);
}
public virtual OnPropertyChanged(string propertyName)
{
}
}
public class ViewModel: NotificationObject
{
private int _MyProperty;
public int MyProperty
{
get { return _MyProperty; }
set
{
if (_MyProperty == value) return;
_MyProperty = value;
RaisePropertyChanged(() => MyProperty);
}
}
override OnPropertyChanged(string propertyName)
{
if (propertyName == "MyProperty")
AdditionalAction();
}
}
This way we don't use event, but all "additional actions" are called from the same "extension point". Is "one place for all addition actions" better than "not transparent workflow"?
I would definitely go for the first method:
it's clear
it's explicit in its flow and intention
it avoids weird (imo) self subscription
The "benefits" of second, which lets you use autogenerated properties is not worth the clearness of the execution flow of the firs case, imo.
Hope this helps.
Here is the "usual" pattern. This allows you to put property-specific code inside the OnX method, and allows derived classes to do the same. No need for a big switch statement, unless of course you're the external listener, but that is par for the course for INotifyPropertyChanged.
public class NotificationObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void FirePropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, e);
}
}
public class ViewModel : NotificationObject
{
private int _MyProperty1;
public int MyProperty1
{
get { return _MyProperty1; }
set
{
if (value != _MyProperty1)
{
_MyProperty1 = value;
OnMyProperty1Changed(new PropertyChangedEventArgs("MyProperty1"));
}
}
}
protected virtual void OnMyProperty1Changed(PropertyChangedEventArgs e)
{
FirePropertyChanged(e);
}
private int _MyProperty2;
public int MyProperty2
{
get { return _MyProperty2; }
set
{
if (value != _MyProperty2)
{
_MyProperty2 = value;
OnMyProperty2Changed(new PropertyChangedEventArgs("MyProperty2"));
}
}
}
protected virtual void OnMyProperty2Changed(PropertyChangedEventArgs e)
{
FirePropertyChanged(e);
}
}

How to add a PropertyChanged event for a property?

I have created various properties inside of a User Control, and have had great success with accessing and editing them. I'm now trying to set up events for a number of these to be raised when one of these properties is changed. I have tried the MSDN example code for doing this (see here), but it is giving me this error when I try to build the solution:
Severity Code Description Project File Line Suppression State
Error CS0079 The event 'AbilityScoreDisplay.AbilityTitleChanged' can only appear on the left hand side of += or -= DnD Character Sheet C:\Users\bradley beasley\Documents\Visual Studio 2019\Projects\DnD Character Sheet\DnD Character Sheet\AbilityScoreDisplay.Designer.cs 199 Active
Another issue that I am having is that I am struggling to figure out how to get that event to appear in the Visual Studio 2019 Designer Properties window.
Here is the code that I have added to the designer file:
namespace DnD_Character_Sheet
{
partial class AbilityScoreDisplay : UserControl
{
public string AbilityTitle
{
get
{
return AbiltyTitleLabel.Text;
}
set
{
AbiltyTitleLabel.Text = value;
Invalidate();
}
}
public int AbilityModifier
{
get
{
return Convert.ToInt32(AbilityModifierTextBox.Text);
}
private set
{
if (value >= 0) AbilityModifierTextBox.Text = String.Format("+{0}", value);
else AbilityModifierTextBox.Text = value.ToString();
Invalidate();
}
}
public int AbilityScore
{
get
{
return Convert.ToInt32(AbilityScoreLabel.Text);
}
set
{
AbilityModifier = (int)(Math.Floor((double)(value) / 2)) - 5;
Invalidate();
}
}
private EventHandler onAbilityTitleChanged { get; set; }
private EventHandler onAbilityScoreChanged { get; set; }
public event EventHandler AbilityTitleChanged
{
add
{
onAbilityTitleChanged += value;
}
remove
{
onAbilityTitleChanged -= value;
}
}
public event EventHandler AbilityScoreChanged
{
add
{
onAbilityScoreChanged += value;
}
remove
{
onAbilityScoreChanged -= value;
}
}
protected virtual void OnAbilityTitleChanged(EventArgs e)
{
AbilityTitleChanged?.Invoke(this, e);
}
protected virtual void OnAbilityScoreChanged(EventArgs e)
{
AbilityScoreChanged?.Invoke(this, e);
}
}
}
The aim is to enable an event to be raised whenever a property is changed so that it can do other stuff elsewhere in the form that the controls will be in. I'm fairly certain that I am missing some very important stuff, or that my code is not that effective at all, but I am learning this kind of code for the first time, and I have tried many different things that have just not worked.
Any help at all would be greatly appreciated :)
I think you are confusing a few concepts. Let's do it step by step.
First, you need to be able to track event handlers:
private EventHandler _onAbilityTitleChanged;
You expose this event through a public property:
public event EventHandler AbilityTitleChanged
{
add
{
_onAbilityTitleChanged += value;
}
remove
{
_onAbilityTitleChanged -= value;
}
}
Finally, you need to fire the event so that all subscribed handlers can react to it. You can do so when the title changes (setter):
public string AbilityTitle
{
get
{
return AbiltyTitleLabel.Text;
}
set
{
AbiltyTitleLabel.Text = value;
//Raising the event!
_onAbilityTitleChanged?.Invoke(this, new EventArgs());
}
}
Other classes can then subscribe to your event:
var control = new AbilityScoreDisplay();
control.AbilityTitleChanged += SomeHandlerForWhenTitleChanges;
private void SomeHandlerForWhenTitleChanges(object sender, EventArgs e)
{
//....
}
You might want to read up a bit on the INotifyPropertyChanged interface as well.
You typically do this by implementing INotifyPropertyChanged. This allows you to use one single event for all the properties. The property name is passed in the event arguments.
partial class AbilityScoreDisplay : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
...
}
In the properties do this (with AbilityModifier as an example):
private int _abilityModifier;
public int AbilityModifier
{
get { return _abilityModifier; }
private set {
if (value != _abilityModifier) {
_abilityModifier = value;
AbilityModifierTextBox.Text = value >= 0
? String.Format("+{0}", value)
: value.ToString();
OnPropertyChanged(nameof(AbilityModifier));
}
}
}
Assuming this event handler
private void ScoreDisplay_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
...
}
You can subscribe the event with
PropertyChanged += ScoreDisplay_PropertyChanged;
You need to use the add/remove syntax only in rare cases. Typically, when you create your own event store, because you have a lot of events and don't want to consume space for unsubscribed events.
You can use INotifyPropertyChanged together with data binding to immediately update the UI when changes are made to the data. To do this you would create a class with properties and the INotifyPropertyChanged implementation. In the form you then assign an instance of this class to the DataSource of a BindingSource. The controls are then bound to this BindingSource.
Then you can drop all the code used to read from or to write to text boxes or labels etc., as the binding mechanism does it automatically for you.

INotifyPropertyChanged does't work when field of property change internally

I try to binding textblock usercontrol with property of my class, but it only works at initial stage, I have implement IPropertyChnaged in my class.
In my class, _Feedbackpos (field of property) would change in background, I don't know how to solve this problem.
my class
public class TestControl : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyname)
{
if(PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyname));
}
}
private double _Feedbackpos;
public double Feedbackpos
{
get
{
return _Feedbackpos;
}
set
{
_Feedbackpos = value;
NotifyPropertyChanged("Feedbackpos");
}
}
//it's a callback function, it would excute when detect feedback position of controller change
private void ReadFeedbackpos()
{
_Feedbackpos = Controller.Read();
}
}
application windows
TestControl TestDll = new TestControl();
Binding BindingTxtBlk = new Binding(){Source= TestDll, Path = new Property("Feedbackpos")};
FeedbackPosTxtBlk.Setbinding(Textblock.TextProperty,BindingTxtBlk);
Change the function ReadFeedbackpos() to
private void ReadFeedbackpos()
{
Feedbackpos = Controller.Read();
}
Otherwise NotifyPropertyChanged("Feedbackpos"); will never get called.

MVVM Light - RaisePropertyChanged not working

I have a ViewModel that is bound to one page called SettingsView.
In this page i have some properties like so:
public string SettingsHeaderTitle { get { return AppResources.settings_header_title; } }
I have one button that navigates to another page where we can change language and then goes back to SettingsPage.
I had implemented a command like so:
public void UpdateView()
{
RaisePropertyChanged(string.Empty);
}
My problem is that when I call this command from on Loadedd or NavigatedTo events nothing happens. Then I added a button to call this command (for debug purposes) and the Page is updated successfully.
What am I doing wrong?
You need to implement INotifyPropertyChanged on your ViewModels like so
public class SelectionItem<T> : INotifyPropertyChanged
{
private AppliesTo _appliesTo;
public AppliesTo AppliesTo
{
get { return _appliesTo; }
set
{
_appliesTo = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("AppliesTo"));
}
}
}
EDIT: I just noticed you're using MVVM Light, then it becomes even easier, in your ViewModels inherit from ViewModelBase and make your property like this:
private bool _isComparisonRun;
public bool IsComparisonRun
{
get { return _isComparisonRun; }
set
{
if (_isComparisonRun == value) return;
_isComparisonRun = value;
RaisePropertyChanged(() => IsComparisonRun);
}
}

Binding variable to a textblock in Windows Phone 8

In XAML, i have a textblock
<TextBlock x:Name="block" Text="{Binding b1}"/>
and in c# i created a property
public int _b1;
public int b1
{
get { return _b1; }
set
{
_b1 = value;
}
}
public MainPage()
{
InitializeComponent();
block.DataContext = this;
}
this worked fine, textblock show the _b1. But when i add a button to chage the _b1 variable
private void bt_click(object sender, RoutedEventArgs e)
{
_b1 = 4;
}
the textblock didn't update ?????
To add to dotNet's answer (which is the correct answer), use a baseclass where you implement INotifyPropertyChanged if you want to avoid redundand code: (this is one example, there are other ways to implement this)
public abstract class BindableBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Equals(storage, value)) { return false; }
storage = value;
OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var eventHandler = PropertyChanged;
if (eventHandler != null)
{
eventHandler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
And use it like so:
class MyClass: BindableBase
{
private int _b1;
public int B1
{
get { return _b1; }
set { SetProperty(ref _b1, value); }
}
}
For UI to update automatically upon property value change, your property needs to either be a DependencyProperty or your class needs to implement INotifyPropertyChanged interface.
For creating a DependencyProperty, you could use Visual Studio's propdp snippet (type propdp inside your class and press Tab) and fill in respective values. If you want to go INotifyPropertyChanged path, you'll need to write the following code in the setter of your property (AFTER setting the value of _b1):
if(PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("b1"));

Categories