How to use TwoWay binding from UserControl?

I have my own UserControl, LabeledTextBox , which is a combination of Label and a..well, TextBox . This control has two properties: Caption , which will be associated with the Label header and Value , which will be bound to the Text TextBox .

the code:

 public class LabeledTextBox : Control { static LabeledTextBox() { DefaultStyleKeyProperty.OverrideMetadata(typeof(LabeledTextBox), new FrameworkPropertyMetadata(typeof(LabeledTextBox))); } public string Caption { get { return (string)GetValue(CaptionProperty); } set { SetValue(CaptionProperty, value); } } // Using a DependencyProperty as the backing store for Caption. This enables animation, styling, binding, etc... public static readonly DependencyProperty CaptionProperty = DependencyProperty.Register("Caption", typeof(string), typeof(LabeledTextBox), new UIPropertyMetadata("")); public string Value { get { return (string)GetValue(ValueProperty); } set { SetValue(ValueProperty, value); } } // Using a DependencyProperty as the backing store for Value. This enables animation, styling, binding, etc... public static readonly DependencyProperty ValueProperty = DependencyProperty.Register("Value", typeof(string), typeof(LabeledTextBox), new UIPropertyMetadata("")); } 

XAML:

 <Style TargetType="{x:Type local:LabeledTextBox}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type local:LabeledTextBox}"> <Grid> <Grid> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Label Grid.Row="0" Content="{TemplateBinding Caption}" /> <TextBox Name="Box" Margin="3,0,3,3" Grid.Row="1" Text="{Binding Value, RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay}" /> </Grid> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style> 

Using:

 <uc:LabeledTextBox Caption="Code:" Value="{Binding ExpenseCode}" /> 

I originally thought I found my answer here: WPF TemplateBinding vs RelativeSource TemplatedParent

The distinction between TemplateBinding and RelativeSource TemplatedParent detailed here. I changed my code accordingly, but it still seems like I'm skipping a step. OneWay binding really works, my text box is bound to the Value property, but the changes are not logged.

How do I make this work?

+6
source share
2 answers

Change the mode here.

 <uc:LabeledTextBox Caption="Code:" Value="{Binding ExpenseCode,Mode=TwoWay}" /> 

he worked on my end

+6
source

Just in case anyone has this problem:

Another approach (perhaps more elegant) is to declare the usercontrol dependency property so that it defaults to two binding methods (for example, the default for a TextBox).

This can be achieved as follows (taken from fooobar.com/questions/913711 / ... answer):

  public DependencyProperty SomeProperty = DependencyProperty.Register("Some", typeof(bool), typeof(Window1), new FrameworkPropertyMetadata(default(bool), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault)); 

The key here is used by FrameworkPropertyMetadata .

+4
source

Source: https://habr.com/ru/post/913708/


All Articles