将Textbox.Text绑定到自定义控件DP

时间:2014-03-03 14:37:55

标签: c# .net wpf data-binding custom-controls

我需要一个自定义控件(CC),其中CC中文本框的Text属性可以绑定到CC上的DependancyProperty。

我还尝试将textbox.text与templatebinding绑定。

我尝试了几乎所有我能想到的,该怎么办? :

泛型中的自定义控件:

<Style TargetType="{x:Type local:TextboxCC}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:TextboxCC}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}">
                        <TextBox Text="{Binding NText, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:TextboxCC}}}"  />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

CC.cs

public class TextboxCC : Control
{
    public string NText
    {
        get { return (string)GetValue(NTextProperty); }
        set { SetValue(NTextProperty, value); }
    }

    // Using a DependencyProperty as the backing store for NText.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty NTextProperty =
        DependencyProperty.Register("NText", typeof(string), typeof(TextboxCC), new PropertyMetadata(null));

    static TextboxCC()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(TextboxCC), new FrameworkPropertyMetadata(typeof(TextboxCC)));
    }
}

主窗口:

Label正好用于检查Mainwindow.Test中的值是否已更改

<local:TextboxCC NText="{Binding Test,Mode=TwoWay}" HorizontalAlignment="Left" Margin="115,99,0,0" VerticalAlignment="Top" Width="206"/>
    <Label Content="{Binding Test,Mode=TwoWay}" HorizontalAlignment="Left" Margin="115,161,0,0" VerticalAlignment="Top" Width="206"/>

Mainwindow.cs

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private string _test;

    public string Test
    {
        get { return _test; }
        set
        {
            _test = value;
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("Test"));
        }
    }

    public MainWindow()
    {
        InitializeComponent();this.DataContext = this;

    }

    public event PropertyChangedEventHandler PropertyChanged;
}

3 个答案:

答案 0 :(得分:3)

默认情况下,只有当控件失去焦点时,才会更新TextBox&#39; Text属性的绑定(在源方向上)。如果您希望每次文本更改时都更新它,您可以将绑定的UpdateSourceTrigger属性设置为PropertyChanged

<TextBox Text="{Binding NText,
    RelativeSource={RelativeSource FindAncestor, AncestorType=local:TextboxCC},
    UpdateSourceTrigger=PropertyChanged}" />

答案 1 :(得分:0)

您需要实现以下内容:

void ExtendedCheckBox_Checked(object sender, RoutedEventArgs e)
    {
        if (!chk_IsChanging)
            this.IsCheckedReal = "X";                      

    }

    public string IsCheckedReal
    {
        get { return (string)GetValue(IsCheckedRealProperty); }
        set
        {
            SetValue(IsCheckedRealProperty, value);
        }
    }

    public static readonly DependencyProperty IsCheckedRealProperty =
        DependencyProperty.Register("IsCheckedReal", typeof(string), typeof(ExtendedCheckBox), new PropertyMetadata(IsCheckedRealChanged));

    private static void IsCheckedRealChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {

        if (e.NewValue.Equals("X") || e.NewValue.Equals("Y"))
        {
            ((ExtendedCheckBox)o).IsChecked = true;
        }
        else if (e.NewValue.Equals("") || e.NewValue.Equals("N"))
        {
            ((ExtendedCheckBox)o).IsChecked = false;
        }


    }

我想要检查复选框时的行为,我想在我的模型属性中使用“X”标记。

所以我创建了一个依赖属性IsCheckedRealProperty,我将这样绑定:

chk.SetBinding(ExtendedCheckBox.IsCheckedRealProperty, binding);

希望这有帮助。

答案 2 :(得分:0)

将依赖项属性的实现更改为此,您的代码将按原样运行。文本框会在丢失焦点时更新。

public static readonly DependencyProperty NTextProperty = DependencyProperty.Register(
       "NText", typeof (string), typeof (TextboxCC), new PropertyMetadata(default(string)));

   public string NText
   {
       get { return (string) GetValue(NTextProperty); }
       set { SetValue(NTextProperty, value); }
   }