6

私は自分のUserControlを持っています。これはaとa..wellLabeledTextBoxの組み合わせです。このコントロールには2つのプロパティがあります。1つはのキャプションにバインドされ、もう1つはのキャプションにバインドされます。LabelTextBoxCaptionLabelValueTextTextBox

コード:

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プロパティにバインドされますが、変更は登録されません。

これを機能させるにはどうすればよいですか?

4

2 に答える 2

7

ここでモードを変更します。

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

それは私の終わりに働いた

于 2012-04-20T10:04:31.793 に答える
5

誰かがこの問題を抱えている場合に備えて:

別のアプローチ(おそらくより洗練された)は、デフォルトで双方向バインディングになるように、ユーザーコントロールの依存関係プロパティを宣言することです(たとえば、フレームワークTextBoxがデフォルトで行うように)。

これは次のように達成できます(このStackoverflowの質問の回答から取得):

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

ここで重要なのは、を使用することFrameworkPropertyMetadataです。

于 2014-01-17T20:21:09.417 に答える