3

我正在尝试制作一个用户控件,DependencyProperties其中有一些转发给用户控件中的子控件。经过几次尝试,我得到了这个工作。为测试做了一个小例子。

在示例中,我有一个名为的用户控件,Ctrl它只包含 aTextBox并公开Text. 此控件用于包含绑定到同一属性的TextBoxaTextBox和我的自定义的窗口中。Ctrl

用户控制

XAML

<UserControl x:Class="trying.Ctrl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    DataContext="{Binding RelativeSource={RelativeSource self}}"
    Height="Auto" Width="Auto">
    <TextBox Text="{Binding MyText}" />
</UserControl>

背后的代码

using System.Windows;
using System.Windows.Controls;

namespace trying
{
    public partial class Ctrl : UserControl
    {
        public static readonly DependencyProperty MyTextProperty = DependencyProperty.Register(
            "MyText",
            typeof( string ),
            typeof( UserControl ),
            new FrameworkPropertyMetadata( default(string), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
        public string MyText
        {
            get { return (string)GetValue( MyTextProperty ); }
            set { SetValue( MyTextProperty, value ); }
        }

        public Ctrl()
        {
            InitializeComponent();
        }
    }
}

窗户

XAML

<Window x:Class="trying.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cc="clr-namespace:trying"
    DataContext="{Binding RelativeSource={RelativeSource self}}"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <TextBox Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center" Text="{Binding DisplayText}" />
        <cc:Ctrl Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" MyText="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DisplayText}" />
    </Grid>
</Window>

后面的代码

using System.Windows;
using System.ComponentModel;

namespace trying
{
    public partial class Window1 : Window, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged( string propertyName )
        {
            if( PropertyChanged != null )
                PropertyChanged( this, new PropertyChangedEventArgs( propertyName ) );
        }

        private string m_displayText = "asdf";
        public string DisplayText
        {
            get { return m_displayText; }
            set
            {
                m_displayText = value;
                NotifyPropertyChanged( "DisplayText" );
            }
        }

        public Window1()
        {
            InitializeComponent();
        }
    }
}

问题

我如何发布它的工作代码。我现在的问题是:我做错了什么,我必须使用绑定

 MyText="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}

绑定MyText属性的时候Ctrl,能不能像我绑定原来的时候那样只用简单的绑定TextBox

如果我不以这种方式绑定,它将不起作用,我会收到警告

  System.Windows.Data Error: 39 : BindingExpression path error: 'DisplayText' property
  not found on 'object' ''Ctrl' (Name='')'. BindingExpression:Path=DisplayText; 
  DataItem='Ctrl' (Name=''); target element is 'Ctrl' (Name=''); target property
  is 'MyText' (type 'String')

我有什么需要改变原来的绑定是可能的TextBox

在执行期间。

4

1 回答 1

2

UserControl's指向自身,因此控件实例上的DataContext任何绑定都将查看Ctrl实例而不是继承的DataContext.

尝试将DataContext树设置得更远:

<UserControl 
   x:Class="trying.Ctrl" x:Name="root"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Height="Auto" Width="Auto"
>
   <TextBox 
      DataContext="{Binding ElementName=root}"
      Text="{Binding MyText}" 
   />
</UserControl>
于 2012-11-09T13:27:06.767 回答