Binding not working when using custom controls?
Hi,
so I'm having a trouble when it comes to bindings. As I'm using three different XAMLs, I will try to make it as understandable and brief as possible.
The structure looks like this: MainView.xaml -- presents--> IntBoxTemplate.xaml -- presents--> IntTextBox.xaml
Here is how it looks visually. The background is MainView.xaml, A B C belong to the IntBoxTemplate and D E F are part of the IntTextBox. The goal is that if the value is changed in either of the two components, that the other one updates as well.
The IntTextBox is an element that only has code behind and has a couple of bindings. The important one is Value, which is always triggering the PropertyChanged event. It works like a charm :D
I added this to my IntBoxTemplate.
The MainView.xamluses a ContentPresenter to show The IntBoxTemplate like this
I suspect that the problem is about setting the view/DataContexts, but I have no idea.
I tried to keep myself short and left out a lot of code that I deemed irrelevant (e.g. the code for the buttons), so if more information is needed I will gladly provide more.
Thanks in advance.
Sincerely,
David
so I'm having a trouble when it comes to bindings. As I'm using three different XAMLs, I will try to make it as understandable and brief as possible.
The structure looks like this: MainView.xaml -- presents--> IntBoxTemplate.xaml -- presents--> IntTextBox.xaml
Here is how it looks visually. The background is MainView.xaml, A B C belong to the IntBoxTemplate and D E F are part of the IntTextBox. The goal is that if the value is changed in either of the two components, that the other one updates as well.
The IntTextBox is an element that only has code behind and has a couple of bindings. The important one is Value, which is always triggering the PropertyChanged event. It works like a charm :D
Code: Select all
public class IntTextBox : UserControl, INotifyPropertyChanged {
public int Value {
get { return (int)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
"Value", typeof(int), typeof(IntTextBox), new PropertyMetadata(0, OnValueChangedCallBack));
public event PropertyChangedEventHandler PropertyChanged;
/*
Removed all the initialization code and the code that actually changes the value for simplicity
*/
protected static void OnValueChangedCallBack(DependencyObject sender, DependencyPropertyChangedEventArgs e) {
IntTextBox intTextBox = sender as IntTextBox;
intTextBox?.OnPropertyChanged("Value");
}
protected void OnPropertyChanged(string name) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) {
handler(this, new PropertyChangedEventArgs(name));
}
}
}
Code: Select all
<MyControl:IntTextBox Value="{Binding Value}"/>
Code: Select all
public class IntTextBoxViewScript : INotifyPropertyChanged {
public event PropertyChangedEventHandler PropertyChanged;
private int _value;
public int Value { get { return _value; } set{ _value = value; OnPropertyChanged("Value"); }}
}
Code: Select all
...
<TextBlock Text="{Binding Value}"/>
...
<ContentPresenter ContentTemplate="{StaticResource IntBoxTemplate}" Content="{Binding IntBoxTemplateScriptObject}"/>
Code: Select all
public class EngineConfigViewScript : MonoBehaviour {
private IntTextBoxViewScript _intBoxTemplateScriptObject = new IntTextBoxViewScript();
public IntTextBoxViewScript IntBoxTemplateScriptObject{ get => _intBoxTemplateScriptObject; }
private void Start() {
NoesisView view = GetComponent<NoesisView>();
view.Content.DataContext = this;
}
}
I tried to keep myself short and left out a lot of code that I deemed irrelevant (e.g. the code for the buttons), so if more information is needed I will gladly provide more.
Thanks in advance.
Sincerely,
David
-
-
sfernandez
Site Admin
- Posts: 2869
- Joined:
Re: Binding not working when using custom controls?
Hi,
Are the -/+ buttons setting the TextBlock.Text property or IntTextBox.Value property? Because as the bindings initially set there are not TwoWay, setting those properties will clear the binding and locally set the new value. If you are only modifying the view model Value property, then it is fine.
If you indicate in which specific situation (pressing one of buttons, writing a value in a textbox...) the values are not updated in all places I can better narrow the problem.
On a side note, in your IntTextBox user control you don't need to implement the INotifyPropertyChanged interface, as dependency properties already notify when they change. That interface is usually only necessary for view models.
Are the -/+ buttons setting the TextBlock.Text property or IntTextBox.Value property? Because as the bindings initially set there are not TwoWay, setting those properties will clear the binding and locally set the new value. If you are only modifying the view model Value property, then it is fine.
If you indicate in which specific situation (pressing one of buttons, writing a value in a textbox...) the values are not updated in all places I can better narrow the problem.
On a side note, in your IntTextBox user control you don't need to implement the INotifyPropertyChanged interface, as dependency properties already notify when they change. That interface is usually only necessary for view models.
Re: Binding not working when using custom controls?
Hi,
So referring to the picture in the post:
The buttons A and C control the TextBlock Value of B.
The buttons D and F control the Textbox Value of E.
Both Values are tight to the same variable in IntTextBoxViewScript.
Therefore I was expecting that if one of the values changes, the other one should get updated as well.
In this case the solution was to add "Mode=TwoWay" to the binding to both the TextBox and the TextBlock. Like this:
I've never used the Mode Attribute before, but I can see why it's needed here.
And thanks for the tip with not needing the INotifyPropertyChanged on the IntTextBox :)
Sincerely,
David
So referring to the picture in the post:
The buttons A and C control the TextBlock Value of B.
The buttons D and F control the Textbox Value of E.
Both Values are tight to the same variable in IntTextBoxViewScript.
Therefore I was expecting that if one of the values changes, the other one should get updated as well.
In this case the solution was to add "Mode=TwoWay" to the binding to both the TextBox and the TextBlock. Like this:
Code: Select all
Text="{Binding Value, Mode=TwoWay}"
And thanks for the tip with not needing the INotifyPropertyChanged on the IntTextBox :)
Sincerely,
David
-
-
sfernandez
Site Admin
- Posts: 2869
- Joined:
Re: Binding not working when using custom controls?
Glad to be helpful :)
Who is online
Users browsing this forum: Bing [Bot], Semrush [Bot] and 1 guest