
I'm trying to use visual helper to fine and change the textblock in the ContentPresenter in the TileViewItem
RadTileViewItem myRadTileViewItem =(RadTileViewItem)(RadTileView1.ItemContainerGenerator.ContainerFromItem(RadTileView1.Items.CurrentItem));
// Getting the ContentPresenter of RadTileView1
ContentPresenter myContentPresenter = FindVisualChild<
ContentPresenter
>(RadTileView1);
// Finding textBlock from the DataTemplate that is set on that ContentPresenter
DataTemplate myDataTemplate = myContentPresenter.ContentTemplate;
TextBlock myTextBlock = (TextBlock)myDataTemplate.FindName("Datext", myContentPresenter);
// Do something to the DataTemplate-generated TextBlock
this.Datext.Text = 5;
<
DataTemplate
x:Key
=
"MyDataTemplate"
>
<
StackPanel
>
<
TextBlock
x:Name
=
"Datext"
FontSize
=
"18"
Text
=
"{Binding}"
/>
<
TextBox
x:Name
=
"Datext1"
FontSize
=
"10"
Text
=
"5 KillDate Items"
/>
</
StackPanel
>
</
DataTemplate
>
private childItem FindVisualChild<
childItem
>(DependencyObject obj)
where childItem : DependencyObject
{
for (int i = 0; i <
VisualTreeHelper.GetChildrenCount
(obj); i++)
{
DependencyObject
child
=
VisualTreeHelper
.GetChild(obj, i);
if (child != null && child is childItem)
return (childItem)child;
else
{
childItem
childOfChild
=
FindVisualChild
<childItem>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
Any thoughts/
Regards,
Rick
8 Answers, 1 is accepted
One possible way to achieve this scenario is by using our extension method called ChildrenOfType:
private
void
Button_Click(
object
sender, RoutedEventArgs e)
{
RadTileViewItem myRadTileViewItem = (RadTileViewItem)(
this
.tileView1.ItemContainerGenerator.ContainerFromIndex(0));
var headerContentPresenter = myRadTileViewItem.ChildrenOfType<ContentPresenter>().Where(presenter => presenter.Name ==
"HeaderElement"
).FirstOrDefault();
if
(headerContentPresenter !=
null
)
{
TextBlock targetTextBlock = headerContentPresenter.ChildrenOfType<TextBlock>().Where(textBlock => textBlock.Name ==
"TargetTextBlock"
).FirstOrDefault();
if
(targetTextBlock !=
null
)
{
MessageBox.Show(targetTextBlock.Text);
}
}
}
This said, I'd like to warn you that using this extension method is not advised since it might lead to performance issues. Also, by doing:
// Do something to the DataTemplate-generated TextBlock
this
.Datext.Text = 5;
you are going to break any databindings previously established.
Regards,
Kiril Stanoev
the Telerik team

Thank you for the response,
I understand its not the bests way,
If your dynamically adding Tileviewitems and and you want format the content, how would you accomplish it.
So example if there is 30 tiles populated and you want the content to number the tiles, but the number needs to have a certain font size etc how would you address that.?
Here is the code I'm using.
private void button1_Click(object sender, RoutedEventArgs e)
{
this.MonthText.Text = this.Mthcmbo.Text;
RadTileView1.Items.Clear();
this.RadTileView1.MaxColumns = 7;
for (int i = 0; i < DateTime.DaysInMonth(Yearcmbo.SelectedIndex + 1, Mthcmbo.SelectedIndex + 1); i++)
{
this.RadTileView1.FontSize = 12;
RadTileView1.Items.Add(new RadTileViewItem()
{
Header = GetFirstDayOfMonth(Mthcmbo.SelectedIndex + 1).AddDays(i).ToString("dddd"),
Content = GetFirstDayOfMonth(Mthcmbo.SelectedIndex + 1).AddDays(i).ToString(" dd"),
});
}
}
Regards,
Rick
The easies solution is to have a couple of predefined DataTemplates and when you generate the tiles(note: to generate about 30 RadTileViewItems every single time will be very slow, so you'd better generate 31 tiles in advance and then just change their content and if you need less than 31 tiles you can always hide the last ones with the Visibility property) just choose which one you need. Could you please examine the attached project and if you have further questions feel free to ask?
Sincerely yours,
Thank you,
Your right its very slow when the datatemplate is binded.
For plan "b"
How would I bind the header and content to the DateTime method. If a user chooses a calendar date then the 31 tiles will already be there, just the headers and content would change. Then if there is only 28 or 30 days in that month, then that last tile would be invisible?
Thoughts?
Rick

Thank you,
Your right its very slow when the datatemplate is binded.
For plan "b"
How would I bind the header and content to the DateTime method. If a user chooses a calendar date then the 31 tiles will already be there, just the headers and content would change. Then if there is only 28 or 30 days in that month, then that last tile would be invisible?
Thoughts?
Rick
I've updated the sample project, so could you please examine it and if you have further questions feel free to ask?
Greetings,
I must say you way beyond my expectations.
Works great. If you could tell me how to change the header to display the day(monday tuesday)
that would be all
very thankful
Regards,
Rick
You can choose how to display a date by applying a pattern to it(for example the pattern "dddd, dd MMMM" will show something like "Monday, 22 November").I've updated the sample project to use this pattern. so please take a look at it and if you have more questions feel free to ask.
Greetings,