如何在UserControl中使用TwoWay绑定? [英] How to use TwoWay binding from within a UserControl?

查看:128
本文介绍了如何在UserControl中使用TwoWay绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自己的UserControl,一个 LabeledTextBox ,它是一个标签和a..well的组合, TextBox 。该控件有两个属性: Caption ,它们将被绑定到标签 c $ c c c

代码:

  public class LabeledTextBox:Control 
{
static LabeledTextBox()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(LabeledTextBox),新的FrameworkPropertyMetadata(typeof(LabeledTextBox)));
}

public string Caption
{
get {return(string)GetValue(CaptionProperty); }
set {SetValue(CaptionProperty,value); }
}

//使用DependencyProperty作为Caption的后备存储。这使得动画,样式,绑定等...
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); }
}

//使用DependencyProperty作为Value的后备存储。这可以使动画,样式,绑定等...
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 =0Content ={TemplateBinding Caption}/>
< TextBox Name =BoxMargin =3,0,3,3Grid.Row =1Text ={Binding Value,RelativeSource = {RelativeSource TemplatedParent},Mode = TwoWay}/ >

< / Grid>
< / Grid>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< / Style>

用法:

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

最初我以为我在这里找到了我的答案: WPF TemplateBinding vs RelativeSource TemplatedParent



详细说明了 TemplateBinding RelativeSource TemplatedParent 。我已经相应地改变了我的代码,但仍然觉得我失去了一个步骤。 OneWay绑定工作,我的文本框绑定到Value属性,但更改不会注册。



如何让它工作?

解决方案

在此处更改模式。

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

它在我的结束工作


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

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>

Usage:

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

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

That details the difference between TemplateBinding and RelativeSource TemplatedParent. I've changed my code accordingly, but it still feels like I'm missing a step. The OneWay binding does work, my textbox is bound to the Value property, but changes do not register.

How do I get this to work?

解决方案

Change the mode here.

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

it worked at my end

这篇关于如何在UserControl中使用TwoWay绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆