私は自分のUserControlを持っています。これはaとa..wellLabeledTextBox
の組み合わせです。このコントロールには2つのプロパティがあります。1つはのキャプションにバインドされ、もう1つはのキャプションにバインドされます。Label
TextBox
Caption
Label
Value
Text
TextBox
コード:
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>
使用法:
<uc:LabeledTextBox Caption="Code:" Value="{Binding ExpenseCode}" />
当初、私はここで自分の答えを見つけたと思っていました:WPFTemplateBindingとRelativeSourceTemplatedParent
TemplateBinding
との違いについて詳しく説明しRelativeSource TemplatedParent
ます。それに応じてコードを変更しましたが、それでもステップが欠落しているように感じます。OneWayバインディングは機能し、テキストボックスはValueプロパティにバインドされますが、変更は登録されません。
これを機能させるにはどうすればよいですか?