sync templatedParent property with control template item property - c#

Using WPF I'm trying to bind Text property of TextBox to Text property of custom control. Could anyone tell me how to achieve that?
Firstly I have template for control which contains TextBox:
<TextBox x:Name="PART_InputTextBox"
Text="[???]">
</TextBox>
My custom control which uses this template contains DependencyProperty "Text"
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(AutocompleteSelector), new UIPropertyMetadata(null));
public string Text
{
get
{
return (string)GetValue(TextProperty);
}
set
{
SetValue(TextProperty, value);
}
}
My question is: What binding I should use (instead of [???]) in Text property of TextBox, to get two way sync of these properties? I mean that, when CustomControl.Text would be changed I want to change TextBox.Text too and vice-versa. I've tried already
{Binding Text, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}
but it doesn't work.

Related

A binding can only be set on a DependencyProperty of a DependencyObject?

I try to develop a UserControl look like a TextBox white two different changes.
First of all the new TextBox has to display a "PlaceholderText" if the TextBox text value is empty. My solution for this implementation includes a second TextBox white the "PlaceholderText" as simply Text Attribute. At last I changed the visibility an the focus to the other TextBox.
An when the Textbox ValidationResult Object return false they display a TextBlock white an "ErrorMessage"
They tow implementations are already working and existent. For my new TextBox I copied all the TextBox specific properties into my new control and passed them to the original TextBox.
Now I tried to bind the Text property from my new control to a DependencyPropery Object (in the ViewModel).
My implementation looks this:
Custom TextBox Text property
public string Text
{
get => TbSource.Text;
set => TbSource.Text = value;
}
ViewModel propdp
public static DependencyProperty PersonProperty =
DependencyProperty.Register(nameof(Person), typeof(Person), typeof(PersonViewModel));
public Person Person
{
get => (Person)GetValue(PersonProperty);
set => SetValue(PersonProperty, value);
}
And my view
<customControl:NiceTextBox Grid.Row="0" Grid.Column="1" IsPlaceholderAktive="True" PlaceholderText="Enter first name" ErrorMessage="The given first name isn't valid." Text="{Binding Person.Name}" />
Now in the implementation in the View I became follow message:
Has anyone an idea how to fix it? I tried to change my Text property to a dependency property but then I can't pass the input and output from the TbSource.
The Text property of your custom control - the target property - must be a dependency property for you to be able to bind to it like this in XAML:
<customControl:NiceTextBox ... Text="{Binding Person.Name}" />
But the Person property in the view model - the source property - shouldn't be defined as a dependency property.
So you have defined the dependency property in the wrong class. Only target properties must be defined as dependency property for you to be able to bind them to some source property.
A control inherits from a DependencyObject class where the GetValue and SetValue methods are defined but a view model generally doesn't.
Make your UserControl Text property as DependencyProperty and the property in ViewModel as a normal CLR property and bind it.
UserControl
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(NiceTextBox), new PropertyMetadata(string.Empty));
ViewModel
public Person Person { get; set; }
XAML
<customControl:NiceTextBox ... Text="{Binding Person.Name}" />

UWP: Passing value from UserControl to MainPage

I have created a UserControl where everything is done in the UserControl.xaml.cs and I want a specific property (called "Value") from the UserControl to be passed to a TextBlock which is created in the MainPage. To test the access of the property, I have created a TextBlock within the UserControl and Bind to Text to "Value" via Text={Binding Path=Value} and it works fine. How do I have to bind the TextBlock from the MainPage to achieve the same?
You might be able to use the ElementName part of the Binding to access the Value from the UserControl. To do this you'll have to give your UserControl an x:Name then set up your Binding like so:
Text="{Binding Value, ElementName=MyUserControl}"
Make sure you have created your Property as a DependencyProperty. You can do it using below code
public string Value
{
get { return (string)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("Value", typeof(string), typeof(UserControl ), new PropertyMetadata(""));
You can get the value in XAML using below code
<TextBlock Text="{Binding ElementName=UserControl, Path=Value}"/>
(OR)
<TextBlock Text="{x:Bind CustomInkControl.Value, Mode=OneWay}"/>
Note: Use x:Bind because It is efficient than Binding

Wpf usercontrol with embedded button : change button's content

I have a simple usercontrol with a button in it which i modified.
When I add this usercontrol to my mainwindow, I can only access the usercontrol's properties. How can I access the button content ? Ideally I'd like to have a custom property let's say "TheText" and I changed it like that
<local:MyButtonControl TheText="My text here will be the button content">
This is what I have in the usercontrol "MyButtonControl"
public object TheText
{
get => (object)GetValue(_text);
set => SetValue(_text, value);
}
public static readonly DependencyProperty _text =
DependencyProperty.Register("Text", typeof(object), typeof(MyButton), new UIPropertyMetadata(null));
But what Am I supposed to put for binding ? Can't figure it out. Here's the concerned button.
<Button x:Name="button" Content="{Binding ??? }" Style="{StaticResource RoundedButton}"/>
The Binding should look like this:
<Button Content="{Binding Text,
RelativeSource={RelativeSource AncestorType=UserControl}}" .../>
Note that a correct dependency property declaration would have to use the same name for both the dependency property and the CLR wrapper. There is also a convention to name the identifier field as <PropertyName>Property.
public object Text
{
get => (object)GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(object), typeof(MyButton));
You should certainly also use string as type of a property that is called Text. Or you call the property ButtonContent or something like that.

Binding from DependencyProperty of a UserControl not working

I have a UserControl with one DependencyProperty which sets in codebehind (I guess this may be a source of my problem, but still don't know what to do):
UserControl
public partial class MyControl
{
public MyControl()
{
InitializeComponent();
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MyControl),
new FrameworkPropertyMetadata("",FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); InvokePropertyChanged(new PropertyChangedEventArgs("Text"));}
}
public static string GetText(DependencyObject obj)
{
return (string)obj.GetValue(TextProperty);
}
public static void SetText(DependencyObject obj, string value)
{
obj.SetValue(TextProperty, value);
}
private void ChangeText()
{
Text="some value";
}
}
In my View.xaml I use this control like this:
<MyControl Text="{Binding Text, RelativeSource={RelativeSource Self}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
And the Text property in my ViewModel:
private string _text;
public string Text
{
get { return _text; }
set { _text= value; InvokePropertyChanged(new PropertyChangedEventArgs("Text"));}
}
The problem:
Text property in the ViewModel never gets updated; when use binding with a regular control like TextBox, all works perfect; if I set Text in XAML, Text propery of UserControl updates.
What I did wrong?
UPDATE
My issue was that I have set DataContext explicitly on MyControl.
Issue is in your Binding:
Text="{Binding Text, RelativeSource={RelativeSource Self},
UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
Text property is in your ViewModel but you are referring to itself by using RealtiveSource to point back to self. So, it's binding Text DP with itself.
If you have set DataContext of your control, it will automatically inherit DataContext from parent. So, you don't need RelativeSource at all.
It simply should be:
Text="{Binding Text}"
Few points more (but not related to your issue):
Since you target to use this property from within control, so go for normal DP instead of attached property.
Since at time of registration, you have set it to bind TwoWay by default. No need to explicitly do that at time of binding.
Remove InvokePropertyChanged call from your DP wrapper setter. Setter won't be called from XAML and also DP is already PropertyChanged aware.
UPDATE
In case DataContext of MyControl is set to instance of another class, above approach will search for Text property in MyControl DataContext.
You can pass DataContext of parent control (StackPanel in your case) like this:
Text="{Binding DataContext.Text, RelativeSource={RelativeSource
Mode=FindAncestor, AncestorType=StackPanel}}"
You have registered your property as attached, yet you are also using it as a regular DependencyProperty. I think that the xaml parser gets confused. Decide which one you want to use.

TemplateBinding not working for textbox text

I have a custom control called EnhancedTextBox which is a UserControl that has a TextBox and a Button. To the consumer I want it to mostly look like a TextBox, so I did the following:
<UserControl.Template>
<ControlTemplate TargetType="textBoxes:EnhancedTextBox">
...
<TextBox Text="{TemplateBinding Text}"...
And in EnhancedTextBox I have
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof (String), typeof (EnhancedTextBox));
public String Text
{
get { return (String) GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
Yet, when I use it as the following:
<EnhancedTextBox Text="{Binding MyText, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}}" />
Then, MyText is never updated, as well as I inspect EnhancedTextBox.Text and it is null. What am I missing? I have been staring at this for a bit and can't figure out what is wrong. I even thought it might be the fact that I was using the same name, so create a property called Text1 which did not work....
Also of note, if I use a regular TextBox, then this all works. So, I am fairly certain the problem is with the EnhancedTextBox itself
I figured it out after reading this MSDN about TemplateBinding. Specifically,
A TemplateBinding is an optimized form of a Binding for template scenarios, analogous to a Binding constructed with {Binding RelativeSource={RelativeSource TemplatedParent}}.
So, I decided to do this explicitly...which would allow me to set the UpdateSourceTrigger (still not sure why it doesn't default to PropertyChanged)
<TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Text, UpdateSourceTrigger=PropertyChanged}"....
And, now it is working. TemplateBinding does not even expose these properties....again, not sure why
You are missing the CallBack when you register the property.
Here's a sample code.
public bool IsSelected
{
get { return (bool)GetValue(IsSelectedProperty); }
set { SetValue(IsSelectedProperty, value); }
}
public void IsSelectedChangedCallback()
{
//actions when property changed
}
private static void OnSelectedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
userControl.IsSelectedChangedCallback();
}
public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(MyUserControl), new PropertyMetadata(new PropertyChangedCallback(OnSelectedChanged)));

Categories

Resources