This is a migrated thread and some comments may be shown as answers.

change Datatemplate from code behind

8 Answers 861 Views
TileView
This is a migrated thread and some comments may be shown as answers.
Rick Mueller
Top achievements
Rank 1
Rick Mueller asked on 10 Nov 2010, 10:50 PM
Hello Telerik,

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

Sort by
0
Kiril Stanoev
Telerik team
answered on 11 Nov 2010, 10:59 AM
Hi Rick,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Rick Mueller
Top achievements
Rank 1
answered on 11 Nov 2010, 04:02 PM
Kiril,

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"),
           });
       }
         
   }
This wired up to two comboboxes with year and month int. then it populates certain amount of tile for each month in that year. I want to be able to display the day(int) bold and adjust the margins to make it look like a calendar


Regards,
Rick
0
Zarko
Telerik team
answered on 15 Nov 2010, 09:02 PM
Hi Rick Mueller,

 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,
Zarko
the Telerik team
See What's New in RadControls for WPF in Q3 2010 on Tuesday, November 16, 2010 11:00 AM - 12:00 PM EST or 10:00 PM - 11:00 PM EST: Register here>>
0
Rick Mueller
Top achievements
Rank 1
answered on 15 Nov 2010, 09:29 PM
Zarko,
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
0
Rick Mueller
Top achievements
Rank 1
answered on 15 Nov 2010, 09:36 PM
Zarko,
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
0
Zarko
Telerik team
answered on 18 Nov 2010, 06:02 PM
Hi Rick Mueller,

 I've updated the sample project, so could you please examine it and if you have further questions feel free to ask?

Greetings,
Zarko
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
0
Rick Mueller
Top achievements
Rank 1
answered on 18 Nov 2010, 07:43 PM
Wow,

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
0
Zarko
Telerik team
answered on 22 Nov 2010, 05:34 PM
Hi Rick Mueller,

 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,
Zarko
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
Tags
TileView
Asked by
Rick Mueller
Top achievements
Rank 1
Answers by
Kiril Stanoev
Telerik team
Rick Mueller
Top achievements
Rank 1
Zarko
Telerik team
Share this question
or