Binding not working when using custom controls?
Posted: 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. 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