DavidG
Topic Author
Posts: 11
Joined: 06 Dec 2022, 12:14

Binding not working when using custom controls?

01 Jun 2023, 10:44

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.
Untitled.png
Untitled.png (5.43 KiB) Viewed 626 times
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
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));
            	}
        }
}
I added this to my IntBoxTemplate.
<MyControl:IntTextBox Value="{Binding Value}"/>
public class IntTextBoxViewScript : INotifyPropertyChanged {
    	public event PropertyChangedEventHandler PropertyChanged;
    	
    	private int _value;
    	public int Value { get { return _value; } set{ _value = value; OnPropertyChanged("Value"); }}
    	
}
The MainView.xamluses a ContentPresenter to show The IntBoxTemplate like this
...
<TextBlock Text="{Binding Value}"/>
...
<ContentPresenter ContentTemplate="{StaticResource IntBoxTemplate}" Content="{Binding IntBoxTemplateScriptObject}"/>
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 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
 
User avatar
sfernandez
Site Admin
Posts: 3064
Joined: 22 Dec 2011, 19:20

Re: Binding not working when using custom controls?

01 Jun 2023, 12:54

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.
 
DavidG
Topic Author
Posts: 11
Joined: 06 Dec 2022, 12:14

Re: Binding not working when using custom controls?

01 Jun 2023, 15:24

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:
Text="{Binding Value, Mode=TwoWay}"
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
 
User avatar
sfernandez
Site Admin
Posts: 3064
Joined: 22 Dec 2011, 19:20

Re: Binding not working when using custom controls?

02 Jun 2023, 12:44

Glad to be helpful :)

Who is online

Users browsing this forum: Google [Bot] and 8 guests