2014-02-13 33 views
1

TextBlock駐留在DataTemplate,因此我不能用它的名字來引用它。那麼如何以編程方式將其(例如)Text屬性綁定?如何以編程方式綁定DataTemplate內的控件的(依賴項)屬性?

XAML:

<UserControl x:Class="MyNameSpace.MyCustomControl" ... > 
    ... 
    <ListBox ItemsSource="{Binding Path=ItemsSource}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock/> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
    ... 
</UserControl> 

代碼:

public partial class MyCustomControl : UserControl { 
    ... 

    public static readonly DependencyProperty DataSourceProperty = 
     DependencyProperty.Register("DataSource", typeof (IEnumerable), 
            typeof (MyCustomControl), 
            new PropertyMetadata(default(IEnumerable))); 

    public IEnumerable DataSource { 
     get { return (IEnumerable) GetValue(DataSourceProperty); } 
     set { SetValue(DataSourceProperty, value); } 
    } 

    public static readonly DependencyProperty MemberPathProperty = 
     DependencyProperty.Register("MemberPath", typeof (string), 
            typeof (MyCustomControl), 
            new PropertyMetadata(default(string))); 

    public string MemberPath { 
     get { return (string) GetValue(MemberPathProperty); } 
     set { SetValue(MemberPathProperty, value); } 
    } 
    ... 
    public MyCustomControl() { 
     InitializeComponent(); 

     var binding = new Binding(MemberPath); 
     BindingOperations.SetBinding(/*how do I refer to the TextBlock here ???*/, 
            TextBox.TextProperty, binding); 
    } 
    ... 
} 

預期用法例如:

<my:MyCustomControl DataSource="{Binding Path=SomeModelCollection}" MemberPath="Name" 

哪裏SomeModelCollection就像ObservableCollection<SomeModel>一些數據模型屬性(SomeModel有一個名爲Name屬性)

+0

d'Textblock'打算顯示什麼..什麼是'MemberPath'..什麼是d itemsource爲此.. – Sankarann

+0

@Sankarann - 我添加了'MemberPath' - 它的目的是綁定'Path'屬性從外部(即在我嵌入'MyCustomControl'的XAML中'TextBlock') – Tar

+0

@Sankarann - 也添加了一個預期的用法示例 – Tar

回答

1

您可以使用VisualTreeHelper獲取TextBlock。這種方法將讓你出現在一個ListBoxItem的視覺樹所有TextBlockes:

public IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) 
      where T : DependencyObject 
{ 
    if(depObj != null) 
    { 
     for(int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) 
     { 
      DependencyObject child = VisualTreeHelper.GetChild(depObj, i); 
      if(child != null && child is T) 
      { 
       yield return (T)child; 
      } 

      foreach(T childOfChild in FindVisualChildren<T>(child)) 
      { 
      yield return childOfChild; 
      } 
     } 
    } 
} 

用法:

TextBlock textBlock = FindVisualChildren<TextBlock>(listBoxItem) 
         .FirstOrDefault(); 

但我還是建議做在代碼中做背後的XAML綁定來代替。

如果ItemSourceObservableCollection<MyModel>MyModel包含財產Name,它可以在XAML做過這樣的:

<DataTemplate> 
    <StackPanel Orientation="Horizontal"> 
     <TextBlock Text="{Binding Name}"/> 
    </StackPanel> 
</DataTemplate> 

由於ListBoxItemDataContextMyModel,因此你可以直接像提到綁定到Name屬性以上。

+0

如何在XAML中執行此操作? – Tar

+0

更新了我的答案。看一看。 –

+0

但我將它綁定到'ObservableCollection ',其中'MyModel'有一個名爲'Name'的字符串字段。這不是一個簡單的'ObservableCollection '。 – Tar

相關問題