Find a WPF element inside DataTemplate in the code-behind
I use this function a lot in my WPF programs to find children elements:
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;
}
}
}
Usage:
foreach (var rectangle in FindVisualChildren<Rectangle>(this))
{
if (rectangle.Name == "rectangleBarChart")
{
/* Your code here */
}
}
How to get element in code behind from DataTemplate
So here is a working solution:
public void TestMethod()
{
DataTemplate dt = FlipView5Horizontal.ItemTemplate;
DependencyObject dio = dt.LoadContent();
foreach (var timeLine in FindVisualChildren<TextBlock>(dio)) //FindVisualTree is defined in the question :)
{
if (timeLine.Name == "xxxTB")
{ }
}
}
Now, I am able to load the control at least. (However, I read that this trick should not be used in the overridden method OnApplyTemplate for some reason).
C# - WPF Access elements in a DataTemplate from code behind
Here is your answer Calling Storyboard inside DataTemplate.
But keep in mind that doing that is not a good idea. Accessing any UI Element from your code behind is not a great idea, because it creates a tight coupling between your code and your UI.
I would suggest you to use MVVM instead, and to bind a boolean to run your storyboard as explained here: How to play Storyboard in ViewModel?
Access XAML Control In DataTemplate From CodeBehind?
I would normally recommend not to touch UIElements from code... but the MediaElement is a special case... maybe you should wrap the whole template inside a usercontrol (maybe with some custom DepProps) and that will give you better control over the whole thing.
Edit: Another approach would be to create a Behavior with a couple of properties (such as IsPlaying) and manipulate the mediaelement from there. Then you could use this behavior in the XAML of the DataTemplate, with no need for code behind or usercontrols.
Access an element from DataTemplate
You should not use ContentControl try using ContentPresent
<Grid x:Name="MyGrid">
<ContentPresenter x:Name="ContentControl" Content="{Binding}" Style="{StaticResource MyContentControlStyle}" />
</Grid>
And behind the code you have to explicitly have to say apply template in order to get it
ContentControl.ApplyTemplate();
var dataGrid = ContentControl.ContentTemplate.FindName("dataGridFromDataTemplate", ContentControl) as DataGrid;
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 ListBoxItem
s:
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
Do C# Timers Elapse on a Separate Thread
Working Example of Createjobobject/Setinformationjobobject Pinvoke in .Net
How to Render a Formula in Wpf or Winforms
How to Return Multiple Result Sets with SQLcommand
Task Sequencing and Re-Entracy
When Should I Use "Using" Blocks in C#
How to Call Base.Base.Method()
How to Get Values of Selected Items in Checkboxlist with Foreach in ASP.NET C#
How to Bind a List<String> to a Datagridview Control
Pinvoke for C Function That Returns Char *
Entity Framework Refresh Context
Catching "Maximum Request Length Exceeded"
What's the Difference Between X = X++; VS X++;
"Self Referencing Loop Detected" Exception with JSON.Net