WPF UserControls geliştirirken, bir alt denetimin DependencyProperty UserControl DependencyProperty olarak ortaya çıkarmak için en iyi yolu nedir? Aşağıdaki örnek, bir TextBox'un Text özelliğini bir UserControl içinde nasıl göstereceğimi gösterir. Elbette bunu başarmak için daha iyi/daha basit bir yol var mı? Biz doğrusu UserControl adlandırma ve UserControl'ın adıyla özelliklerini başvurarak, RelativeSource arama olmadan, bizim takımda yapıyoruz nasılExpose 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(); }
}
}
Bu benim için çalışan hayır 'FindAncestor' –
olduğu yerde bu şekilde ancak senin gibi görünmüyor, Silverlight 4 en iyi şekilde çalışır Aynı x: Class' ve x: Name' kullanın. WpfApplication1.SliderLabel gibi bir sınıfa sahip olmalı ve SliderLabelControl gibi bir isim verdim. Aksi takdirde ismin zaten var olduğundan şikayetçi oldu. –