質問

開発時のコンポーネントのラインナップUserControlsは、何が最良の方法なDependencyProperty子制御としてDependencyPropertyのUserControl?以下はその方法を示していますし、現在はさらにテキストのテキストボックス内のUserControl.はあるのではないでしょうかより良い/簡素ではな遂行す。

<UserControl x:Class="WpfApplication3.UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel Background="LightCyan">
        <TextBox Margin="8" Text="{Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
    </StackPanel>
</UserControl>


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

namespace WpfApplication3
{
    public partial class UserControl1 : UserControl
    {
        public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(UserControl1), new PropertyMetadata(null));
        public string Text
        {
            get { return GetValue(TextProperty) as string; }
            set { SetValue(TextProperty, value); }
        }

        public UserControl1() { InitializeComponent(); }
    }
}
役に立ちましたか?

解決

その上で使用できるチームではなく、RelativeSource検索、決めるのではなく、ネーミングのUserControlデ性のUserControlます。

<UserControl x:Class="WpfApplication3.UserControl1" x:Name="UserControl1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <StackPanel Background="LightCyan">
        <TextBox Margin="8" Text="{Binding Path=Text, ElementName=UserControl1}" />
    </StackPanel>
</UserControl>

時として私たちがその場で発音を確認することが自ら作りも多くのことUserControlので、しばしば倍の速てます。いいもの伝統のネーミングというようなテキストボックスのPART_TextDisplayもので、今後のことだがテンプレートではなく、コードの背後に同じです。

他のヒント

設定できますDataContextるこUserControlのコンストラクタは、キャラクター設定を結合するだけです。

CS:

DataContext = this;

ー:

<TextBox Margin="8" Text="{Binding Text} />
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top