如何正确设置一个WPF用户控件绑定为 [英] How to properly set up a WPF UserControl for Binding

查看:115
本文介绍了如何正确设置一个WPF用户控件绑定为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个用户控件,基本上是一个文本框的标签。现在,我希望能够绑定 TextBox.Text 为不同的值。

I want to create a UserControl, that essentially is a Label with a TextBox. Now I want to be able to Bind TextBox.Text to different values.

有关这个我在我的用户创建了一个DependencyProperty的,我现在试图绑定的东西是新创建的DependencyProperty,但文字似乎没有得到更新。

For this I created a DependencyProperty in my UserControl and am now trying to bind something to that newly created DependencyProperty, but the Text seems not to get updated.

我UserControl1.xaml看起来是这样的:

My UserControl1.xaml looks like this:

<UserControl x:Class="WpfApplication1.UserControl1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         mc:Ignorable="d" 
         d:DesignHeight="48" d:DesignWidth="200">
<Grid>
    <WrapPanel Height="48" HorizontalAlignment="Left" Name="wrapPanel1" VerticalAlignment="Top" Width="200">
        <Label Content="Label" Height="48" Name="label1" Width="100" />
        <TextBox Height="48" Name="textBox1" Width="100"  />
    </WrapPanel>
</Grid>

和我UserControl1.xaml.cs是这样的:

And my UserControl1.xaml.cs looks like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Diagnostics;

namespace WpfApplication1
{


    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {
        private string value;
        public string Value
        {
            get { return value; }
            set
            {
                this.value = value;
                textBox1.Text = value;
                Trace.TraceInformation("value set in UserControl1");
            }
        }
        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(string), typeof(UserControl1));
        public UserControl1()
        {
            InitializeComponent();
        }
    }
}

我使用的用户控件是这样的:

I am using the UserControl like this:

<my:UserControl1 x:Name="userControl11" Value="{Binding Path=Name}" />

的DataContext 设置为具有Name属性和实现INotifyPropertyChanged此属性的对象。

with DataContext set to an object that has a Name property and implements INotifyPropertyChanged for this property.

推荐答案

您把文本框的文本和用户控件在错误的位置值之间的连接。在CLR属性用于方便,但它不使用绑定引擎。您需要绑定文本框的文本到用户控件的值明确对XAML或code后面,如(假设你给你的用户控件的名字叫根):

You put connection between TextBox's Text and UserControl's Value in wrong place. The CLR property is used for convenience but it is not used by Bind Engine. You need bind TextBox's Text to Usercontrol's Value explicitly on XAML or code behind, such as (assuming you give your user control a name called root):

<TextBox x:Name="textBox1" Text="{Binding Path=Value, ElementName=root}"/>

这篇关于如何正确设置一个WPF用户控件绑定为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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