How to Access a Control Inside a Xaml Datatemplate

How do I access a control inside a XAML DataTemplate?

The problem you are experiencing is that the DataTemplate is repeating and the content is being generated by the FlipView. The Name is not exposed because it would conflict with the previous sibling that was generated (or the next one that will be).

So, to get a named element in the DataTemplate you have to first get the generated item, and then search inside that generated item for the element you want. Remember, the Logical Tree in XAML is how you access things by name. Generated items are not in the Logical Tree. Instead, they are in the Visual Tree (all controls are in the Visual Tree). That means it is in the Visual Tree you must search for the control you want to reference. The VisualTreeHelper lets you do this.

Now, how to do it?

I wrote an article on this because it is such a recurring question: http://blog.jerrynixon.com/2012/09/how-to-access-named-control-inside-xaml.html but the meat of the solution is a recursive method that looks something like this:

public void TestFirstName()
{
foreach (var item in MyFlipView.Items)
{
var _Container = MyFlipView.ItemContainerGenerator
.ContainerFromItem(item);
var _Children = AllChildren(_Container);

var _FirstName = _Children
// only interested in TextBoxes
.OfType<TextBox>()
// only interested in FirstName
.First(x => x.Name.Equals("FirstName"));

// test & set color
_FirstName.Background =
(string.IsNullOrWhiteSpace(_FirstName.Text))
? new SolidColorBrush(Colors.Red)
: new SolidColorBrush(Colors.White);
}
}

public List<Control> AllChildren(DependencyObject parent)
{
var _List = new List<Control>();
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(parent); i++)
{
var _Child = VisualTreeHelper.GetChild(parent, i);
if (_Child is Control)
_List.Add(_Child as Control);
_List.AddRange(AllChildren(_Child));
}
return _List;
}

The key issue here is that a method like this gets all the children, and then in the resulting list of child controls you can search for the specific control you want. Make sense?

And now to answer your question!

Because you specifically want the currently selected item, you can simply update the code like this:

if (MyFlipView.SelectedItem == null)
return;
var _Container = MyFlipView.ItemContainerGenerator
.ContainerFromItem(MyFlipView.SelectedItem);
// then the same as above...

Access a Named Control Inside a XAML DataTemplate

I have tested your code, but I could not reproduce your issue in my side. As far as I'm concerned, It is low performance to render 10000 items in your GridView. And using VisualTreeHelper will bring about worse performance. You could bind
the text of TextBlock in the datatemplate with mvvm ViewModel. You just need
to modify the view model and the text of TextBlock will be changed. For more please refer to Data binding in depth. And the following is segment code of ViewModel.

MainPageViewModel.cs

public class MainPageViewModel : ViewModelBase
{
private ObservableCollection<Phone> _items;
public ObservableCollection<Phone> Items
{
get
{
return _items;
}
set
{
_items = value;
OnPropertyChanged();
}
}
public MainPageViewModel()
{
var list = new ObservableCollection<Phone>();
for (var i = 0; i < 1000; i++)
{
list.Add(new Phone { PhoneNumber = "123456" });
}
_items = list;
}

}

MainPage.xaml

<Page.DataContext>
<local:MainPageViewModel x:Name="ViewModel"/>
</Page.DataContext>
<StackPanel Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Button Click="btnChangePhoneNumber_Click" Content=" click me"/>
<GridView x:Name="GridCell" Height="400" ItemsSource="{Binding Items}" >
<GridView.ItemTemplate>
<DataTemplate x:DataType="local:Phone">
<TextBlock Text="{x:Bind PhoneNumber ,Mode=OneWay}"/>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
</StackPanel>

I have upload the code sample to github. Please check!

How to get value of XAML Control from DataTemplate

You can get the Button's parent (i.e. StackPanel), and then its parent's parent (i.e. Grid), and then go down and find the TextBox.

But... Don't do this. What if you changed the hierarchy, or the type of the Panel?

Since you already know the type (i.e. Ausstattung) of the DataContext of your data template, you should create another property say TextValue and have it two-way bound with the TextBox. Then, you can either get its value from a CommandParameter if you use Button's Command, or in code-behind -

private void ButtonBase_Click(object sender, RoutedEventArgs e)
{
var button = (ButtonBase)sender;
var dataContext = (Ausstattung)button.DataContext;
var value = dataContext.TextValue;
}

Your class needs to implement INotifyPropertyChanged. After that, create a new property like this -

using System.ComponentModel;
using System.Runtime.CompilerServices;
using App1.Annotations;

namespace App1
{
public class Ausstattung : INotifyPropertyChanged
{
private string _textValue;
public string TextValue
{
get => _textValue;
set
{
_textValue = value;
OnPropertyChanged();
}
}

public event PropertyChangedEventHandler PropertyChanged;

[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}

In your xaml, do this -

<TextBox Text="{x:Bind TextValue, Mode=TwoWay}" Grid.Column="3" Foreground="White" FontSize="14" x:Name="txtAnzahl" PlaceholderText="{x:Bind Anzahl}" TextChanged="TextBox_OnTextChanged" Width="50" HorizontalAlignment="Center" VerticalAlignment="Center"/>

How to access a Control inside the data template in C# Metro UI in the code behind

Try the following:

    private DependencyObject FindChildControl<T>(DependencyObject control, string ctrlName)
{
int childNumber = VisualTreeHelper.GetChildrenCount(control);
for (int i = 0; i < childNumber; i++)
{
DependencyObject child = VisualTreeHelper.GetChild(control, i);
FrameworkElement fe = child as FrameworkElement;
// Not a framework element or is null
if (fe == null) return null;

if (child is T && fe.Name == ctrlName)
{
// Found the control so return
return child;
}
else
{
// Not found it - search children
DependencyObject nextLevel = FindChildControl<T>(child, ctrlName);
if (nextLevel != null)
return nextLevel;
}
}
return null;
}

Then call it from the play / pause button button:

    MediaElement media = FindChildControl<MediaElement>(this, "media") as MediaElement;
media.Play();

A related blog post on the subject

XAML - How do I access a parent control's DataTemplate property from a child in a ControlTemplate?

I realized the answer to my own question was pretty simple.

I just needed to directly set the ContentTemplate.

<Style x:Key="Styling" TargetType="{x:Type local:CustomUserControl}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomUserControl}">

<!-- This works -->
<ContentControl ContentTemplate="{TemplateBinding ItemHeaderTemplate}"/>

<!-- This now works too -->
<TabControl ItemsSource="{Binding Items}"
ContentTemplate="{TemplateBinding ItemHeaderTemplate}"/>

</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

WPF How to access control from DataTemplate

First of all, I can't even find the relation between the Resource (ShowAsExpanded) and the usage inside the ContentPresenter. But for the moment, let's assume that the DynamicResource should point to ShowAsExpanded.

You can't and shouldn't access the combobox via code. You should bind the datacontext to the grid that uses the style. If you don't want to do that, you will have to find the content at runtime and search for the child combobox.

how to access a control within Data Template from code behind?

You should be able to access your control using the FrameworkTemplate.FindName method... first, get the ContentPresenter from one of the ListBoxItems:

ContentPresenter contentPresenter = FindVisualChild<ContentPresenter>(yourListBoxItem);

Then get the DataTemplate from the ContentPresenter:

DataTemplate yourDataTemplate = contentPresenter.ContentTemplate;

Then get the MediaElement from the DataTemplate:

MediaElement yourMediaElement = yourDataTemplate.FindName("vidList", contentPresenter) 
as MediaElement;
if (yourMediaElement != null)
{
// Do something with yourMediaElement here
}

Please see the FrameworkTemplate.FindName Method page on MSDN for more information.



Related Topics



Leave a reply



Submit