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); } }
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?
source share