I've searched the forums but can't find an answer to this problem:
<DataTemplate x:Key="ProductDataTemplate">
<Grid x:Name="gridSelectedProduct">
// here are lots of objects that are data bound
<Grid.Resources>
// here are some storyboards that animate those objects, eg:
<Storyboard x:Name="sbShowLargePhoto">
</Storyboard>
</Grid.Resources>
</Grid>
</DataTemplate>
I cannot seem to access the storyboards in code behind so I can start and stop them etc.
Does anyone know how I can access those resources?
Thanks,
Richard
--------------------------------------
If you can meet with triumph and disaster and treat those two imposters just the same...then you'll be a coder my son.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;
namespace Temp01
{
public partial class Page : UserControl
{
//private int count;private List myGridList = new List();
public Page()
{
InitializeComponent();
Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded(object sender, RoutedEventArgs e)
{
myListBox.Items.Add((new ListBoxItem()).Content = "aaaa");
myListBox.Items.Add((new ListBoxItem()).Content = "bbbb");
myListBox.Items.Add((new ListBoxItem()).Content = "cccc");
}
private void btnStart_Click(object sender, RoutedEventArgs e)
{
GetChildrenWithParent(myListBox, typeof(Grid));
if (myGridList != null && myGridList.Count >0)
{
Storyboard mySB = myGridList[2].Resources["myStoryBoard"] as Storyboard;
if (mySB !=null)
mySB.Begin();
}
}private void GetChildrenWithParent(UIElement parent, Type targetType)
{
int count = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < count; i++)
{
UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
if (child.GetType() == targetType && ((Grid)child).Name == "gridName")
{
myGridList.Add((Grid)child);
}
GetChildrenWithParent(child, targetType);
}
}
private void btnEnd_Click(object sender, RoutedEventArgs e)
{
//myStoryBoard.Stop();
}
}
}
Best regards,
Jonathan
Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
Thanks Jonathan, without the code example I'd still be confused, but now I'm not ;)
--------------------------------------
If you can meet with triumph and disaster and treat those two imposters just the same...then you'll be a coder my son.
rmcsharry
Member
399 Points
289 Posts
How to access resources inside a data template?
Oct 12, 2008 06:06 PM | LINK
Hi,
I've searched the forums but can't find an answer to this problem:
<DataTemplate x:Key="ProductDataTemplate">
<Grid x:Name="gridSelectedProduct">
// here are lots of objects that are data bound
<Grid.Resources>
// here are some storyboards that animate those objects, eg:
<Storyboard x:Name="sbShowLargePhoto">
</Storyboard>
</Grid.Resources>
</Grid>
</DataTemplate>
I cannot seem to access the storyboards in code behind so I can start and stop them etc.
Does anyone know how I can access those resources?
Thanks,
Richard
If you can meet with triumph and disaster and treat those two imposters just the same...then you'll be a coder my son.
Jonathan She...
All-Star
50156 Points
4951 Posts
Microsoft
Re: How to access resources inside a data template?
Oct 14, 2008 07:49 AM | LINK
Hi Richard,
It is a little complex in your condition. Fortunately, we can use VisualTreeHelper to get its visual elements. Below is the working example,
<UserControl x:Class="Temp01.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White" >
<ListBox Width="300" Height="300" x:Name="myListBox">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid x:Name="gridName">
<Grid.Resources>
<Storyboard x:Key="myStoryBoard">
<PointAnimation Storyboard.TargetProperty="Center"
Storyboard.TargetName="MyAnimatedEllipseGeometry"
Duration="0:0:5"
From="20,200"
To="400,100"
RepeatBehavior="Forever" />
</Storyboard>
</Grid.Resources>
<Path Fill="Blue">
<Path.Data>
<!-- Describes an ellipse. -->
<EllipseGeometry x:Name="MyAnimatedEllipseGeometry"
Center="20,20" RadiusX="15" RadiusY="15" />
</Path.Data>
</Path>
<TextBlock Text="{Binding}" Margin="50,10,0,0"></TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel Orientation="Horizontal">
<Button x:Name="btnStart" Content="Start" Height="30" Click="btnStart_Click"></Button>
<Button x:Name="btnEnd" Content="End" Height="30" Margin="50,0,0,0" Click="btnEnd_Click"></Button>
</StackPanel>
</Grid>
</UserControl>
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
rmcsharry
Member
399 Points
289 Posts
Re: How to access resources inside a data template?
Oct 16, 2008 07:31 PM | LINK
Thanks Jonathan, without the code example I'd still be confused, but now I'm not ;)
If you can meet with triumph and disaster and treat those two imposters just the same...then you'll be a coder my son.
Nisarg More
Member
4 Points
2 Posts
Re: How to access resources inside a data template?
Sep 20, 2011 09:55 AM | LINK
Hey Jonathan Just Tell Me How to Find Control inside Of DataGrid Header Column
Here is My XAML Code
<navigation:Page x:Class="CheckBoxDemo.Views.MyPages.CheckBoxDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
xmlns:navigation="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Navigation"
xmlns:dg="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
d:DesignWidth="640" d:DesignHeight="480"
xmlns:dataprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
Title="CheckBoxDemo Page" Loaded="Page_Loaded">
<dg:DataGridTemplateColumn Width="auto">
<dg:DataGridTemplateColumn.HeaderStyle>
<Style TargetType="dataprimitives:DataGridColumnHeader">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<CheckBox Content="Select Location" IsChecked="{Binding IsSelected, Mode=TwoWay}" x:Name="chkSelectAll" Checked="chkSelectAll_Checked" Unchecked="chkSelectAll_Unchecked"></CheckBox>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</dg:DataGridTemplateColumn.HeaderStyle>
<dg:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<CheckBox x:Name="chksSelected" IsChecked="{Binding IsSelected, Mode=TwoWay}" Width="100"VerticalAlignment="Center"/>
</DataTemplate>
</dg:DataGridTemplateColumn.CellEditingTemplate>
</dg:DataGridTemplateColumn>
Now i want to UnCheck that CheckBox located in DataGrid Header on Click Of Button How i can Achieve these Functionality
Any Help Would Be Appreciated