简体   繁体   中英

How to get the text with in the datatemplate?

I have used below code

    <DataTemplate x:Key="myTemplate">
        <TextBlock Text="Hi"></TextBlock>
    </DataTemplate>

in this case i can able to get the textblock text by using below code

DataTemplate myTemplate = this.Resources["myTemplate"] as DataTemplate;
  TextBlock rootElement = myTemplate.LoadContent() as TextBlock;
  //I can get the text "rootElement.text "

but when i use binding means i cant able to get the text

<DataTemplate x:Key="myTemplate">
    <TextBlock Text="{Binding EmployeeName}"></TextBlock>
</DataTemplate>

In order to get access to an element that was defined inside a DataTemplate , we first need to get hold of a ContentPresenter . We can get the ContentPresenter from an item that has had the DataTemplate applied to it. We can then access the DataTemplate from the ContentPresenter and then access its elements using the FindName method. Here is an example from the How to: Find DataTemplate-Generated Elements page on MSDN, which you should read for full details:

// Getting the currently selected ListBoxItem 
// Note that the ListBox must have 
// IsSynchronizedWithCurrentItem set to True for this to work
ListBoxItem myListBoxItem = (ListBoxItem)
    (myListBox.ItemContainerGenerator.ContainerFromItem(myListBox.Items.CurrentItem));

// Getting the ContentPresenter of myListBoxItem
ContentPresenter myContentPresenter = FindVisualChild<ContentPresenter>(myListBoxItem);

// Finding textBlock from the DataTemplate that is set on that ContentPresenter
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
TextBlock myTextBlock = (TextBlock)
    myDataTemplate.FindName("textBlock", myContentPresenter);

// Do something to the DataTemplate-generated TextBlock
MessageBox.Show("The text of the TextBlock of the selected list item: " + 
    myTextBlock.Text);

DataTemplates are like blueprints, that describes how a particular data should be displayed on the screen. DataTemplates are just resources. They can be sshared by multiple elements. DataTemplate are not derived from FrameworkElement , so they cannot have DataContext / Databinding on their own. When you apply a DataTemplate to some element, it is implicitly given an appropriate data context.

So, I guess, you wont be able to get bounded data unless you apply the template to something.

When using DataBinding you CAN get text and even easier. Only thing that you should keep in mind is DataContext of your template. If you're using template in a ListBox - then context will be particular Item ie:

public class Employee : INotifyPropertyChanged
{
    public string EmployeeName 
    {
       get 
       {
          return this.employeeName;
       }
       set 
       {
          this.employeeName = value;
          this.OnPropertyChanged("EmployeeName");
       }
    }

    ...
}

In ViewModel :

public class EmployeeViewModel : INotifyPropertyChanged
{
    public List<Employee> Employees
    {
       get
       {
           return this.employees;
       }
       set
       {
          this.employees = value;
          this.OnPropertyChanged("Employees");
       }
    }

    ...    
}

And xaml :

<ListBox ItemsSource={Binding Employees}>
 <ListBox.ItemTemplate>
   <DataTemplate x:Key="myTemplate">
    <TextBlock Text="{Binding EmployeeName}"></TextBlock>
   </DataTemplate>
 </ListBox.ItemTemplate>
</ListBox>

With such structure you'll have

ListBox    | Value source
Employee0  | (Employees[0].EmployeeName)
Employee1  | (Employees[1].EmployeeName)
...        | ...
EmployeeN  | (Employees[n].EmployeeName)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM