2012-08-10 34 views
17

En basit örnekle bile çalışmayı denemeye çalışırken çıldırıyorum. Hayatımın işleyişine bağlanamam. İşte benim için çalışmayan süper kolay bir örnek. Yanlış bir şey yapmalıyım. Özel Denetim Bağımlılığı Özellik Ciltleme

Benim özel denetim Kontrolüm kütüphane montajında:

public class TestControl : Control 
{ 
    public static readonly DependencyProperty TestPropProperty = 
     DependencyProperty.Register("TestProp", typeof(string), typeof(TestControl), new UIPropertyMetadata(null)); 

    public string TestProp 
    { 
     get { return (string)GetValue(TestPropProperty); } 
     set { SetValue(TestPropProperty, value); } 
    } 

    static TestControl() 
    { 
     DefaultStyleKeyProperty.OverrideMetadata(typeof(TestControl), new FrameworkPropertyMetadata(typeof(TestControl))); 
    } 
} 

Ve XAML şablonu: Burada

<Style TargetType="{x:Type local:TestControl}"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type local:TestControl}"> 
       <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"> 
        <StackPanel> 
         <TextBlock Text="Testing..." /> 
         <Label Content="{Binding TestProp}" Padding="10" /> 
        </StackPanel> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

var benim kontrol kütüphanesine referans içeren bir wpf penceresinde kontrolünü tüketen XAML:

<Grid> 
    <ItemsControl Name="mylist"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate> 
       <my:TestControl TestProp="{Binding Path=Name}" /> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl> 
</Grid> 

Ve burada kod arkasında:

public partial class Test2 : Window 
{ 
    public class TestObject : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 
     protected void OnPropertyChanged(string PropertyName) 
     { 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(PropertyName)); 
     } 

     private int _id; 
     public int id 
     { 
      get { return _id; } 
      set { _id = value; OnPropertyChanged("id"); } 
     } 

     private string _Name; 
     public string Name 
     { 
      get { return _Name; } 
      set { _Name = value; OnPropertyChanged("Name"); } 
     } 
    } 

    public Test2() 
    { 
     InitializeComponent(); 

     mylist.ItemsSource = new TestObject[] 
     { 
      new TestObject(){ id = 1, Name = "Tedd" }, 
      new TestObject(){ id = 2, Name = "Fred" }, 
      new TestObject(){ id = 3, Name = "Jim" }, 
      new TestObject(){ id = 4, Name = "Jack" }, 
     }; 
    } 
} 

Bu örnek çalışmama, kontrolün dört örneğini verir, ancak yalnızca her birinde "Test ..." TextBlock'u görüyorum. Etiketim asla bağlı değil. Neyi yanlış anladım ve yanlış yapıyorum?

cevap

21

Uygun ciltleme kaynağını ayarlamadınız. Sen RelativeSource ayarlamak zorunda kalacak ya:

<Label Content="{Binding TestProp, RelativeSource={RelativeSource Mode=TemplatedParent}}" /> 

Veya kullanmak TemplateBinding:

<Label Content="{TemplateBinding TestProp}"/> 
İlgili konular