문제

을 개발할 때 WPF UserControls,가장 좋은 방법은 무엇입하 노출 DependencyProperty 의 아동으로 통제 DependencyProperty 의 정렬?다음 예는 방법을 보여 줍니다 나는 현재 텍스트를 노출의 속성 텍스트 상자 안쪽 정렬.반드시 더 나은/간단한 방법으로 수행하는 이?

<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 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>

때때로 우리는 자신을 발견하게 만드는 너무 많은 것들의 정렬하지만,그리고 종종 시간 조정을 다시 우리의 사용합니다.나도 전통을 따르의 이름 같은 것들 textbox 의 라인을 따라 PART_TextDisplay 또는 무언가는,그래서 미래에 당신은 수 있는 템플릿에서 그것은 아직 계속 코드의 뒤에 동일합니다.

다른 팁

당신은 설정할 수 있습니다 매핑되 이에 정렬의 생성자,그냥 바인딩에 의해서만 경로입니다.

CS:

DataContext = this;

XAML:

<TextBox Margin="8" Text="{Binding Text} />
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top