簡體   English   中英

將 DependencyProperty 轉發到包含在用戶控件中的控件

[英]forward DependencyProperty to control contained in User Control

我正在嘗試制作一個具有一些DependencyProperties的用戶控件,這些DependencyProperties轉發給用戶控件中的子控件。 經過幾次嘗試,我得到了這個工作。 為測試做了一個小例子。

在這個例子中我有一個名為的用戶控件Ctrl只包含一個TextBox ,並公開Text中的屬性TextBox 此控件用於包含綁定到同一屬性的TextBox和我的自定義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}}

當綁定CtrlMyText屬性時,不能像我在綁定原始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一樣的綁定?

在執行過程中。

UserControlDataContext指向自身,因此控件實例上的任何綁定都將查看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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM