Commanding & Controls

7 posts, 0 answers
  1. UGH!!
    UGH!! avatar
    21 posts
    Member since:
    Nov 2010

    Posted 26 May 2011 Link to this post

    I've been trying to use the TileView for a requirement I have, but I'm having trouble with both Commanding and accessing controls. 

    Can you demonstrate how to access the RadGridView inside the RadTileView??

    Also, when I try to use Command with a button inside the TileView, it will never fire. If I move the button outside the TileView, it works perfectly.  Is there an issue with Commanding within RadTileView??

    Thanks!!
  2. Tina Stancheva
    Admin
    Tina Stancheva avatar
    3298 posts

    Posted 01 Jun 2011 Link to this post

    Hello UGH!!,

    I attached a sample project where the RadTileView control is databound to a collection of business objects. The ContentTemplate of the control defines a RadGridView control. You can access this RadGridView control using the Telerik extension methods for walking through the visual tree, for example when you get the RadTileViewItem you can use it to find for it a child of type RadGridView:
    private void RadTileView_PreviewTileStateChanged(object sender, PreviewTileStateChangedEventArgs e)
    {
        RadTileViewItem tile = e.OriginalSource as RadTileViewItem;
        if (tile.TileState == TileViewItemState.Maximized)
        {
            RadGridView grid = tile.FindChildByType<RadGridView>();
            grid.BorderBrush = new SolidColorBrush(Colors.Green);
            grid.BorderThickness = new Thickness(2);
        }
    }

    Also, I was unable to reproduce the issue with the button's commands so can you please let me know if the approach illustrated in the sample project works for you?

    Greetings,
    Tina Stancheva
    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
  3. DevCraft banner
  4. UGH!!
    UGH!! avatar
    21 posts
    Member since:
    Nov 2010

    Posted 24 Jun 2011 Link to this post

    Well ... I tried that and always get "object reference" errors.  Here's what I did and it's working ...

     

     

    private void _linTileView_TileStateChanged(object sender, Telerik.Windows.RadRoutedEventArgs e)
            {
                RadTileViewItem _tileItem = e.Source as RadTileViewItem;
                if (_tileItem != null)
                {
                    if (_tileItem.TileState == TileViewItemState.Maximized)
                    {
                        _linID = ((LIN)_tileItem.Header).ID;
                        ViewModel.LoadData(_linID);

                        foreach (RadGridView _radGrid in VisualTreeEnumeration.FindVisualChildren<RadGridView>(_tileItem))
                        {
                            RadContextMenu.SetContextMenu(_radGrid, _conditionContextMenu);
                        }

                        foreach (RadButton _radButtons in VisualTreeEnumeration.FindVisualChildren<RadButton>(_tileItem))
                        {
                            if (_radButtons.Name == "_btnNew") _radButtons.Command = ViewModel.AddCommand;
                            if (_radButtons.Name == "_btnEdit") _radButtons.Command = ViewModel.EditCommand;
                            if (_radButtons.Name == "_btnDelete") _radButtons.Command = ViewModel.DeleteCommand;
                        }
                    }               
                }
            }


    Using a static class:

     

    public static class VisualTreeEnumeration
        {
            public static 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;
                        }
                    }
                }
            }
        }

  5. UGH!!
    UGH!! avatar
    21 posts
    Member since:
    Nov 2010

    Posted 24 Jun 2011 Link to this post

    Now .... This works except on the initial onload of the radgridview.  I have the maximizemode=One. I can get the grid to bind to the viewmodel, but cannot seem to get the Commanding to work.  As soon as I change my selection, everything binds properly.  It only is on the initial load.

    Any thoughts??
  6. Zarko
    Admin
    Zarko avatar
    755 posts

    Posted 29 Jun 2011 Link to this post

    Hello UGH!!,
    I guess the problem is that the TileStateChanged event is not risen on initial loading. You can try to handle the StatusChanged event of the ItemContainerGenerator of the RadTileView and in it you can set the ContextMenu and the Command to all RadTileViewItems like this:
    public MainPage()
    {
        InitializeComponent();
     
        this._linTileView.ItemContainerGenerator.StatusChanged += this.ItemContainerGenerator_StatusChanged;
    }
     
    void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
    {
        Telerik.Windows.Controls.ItemContainerGenerator generator = sender as Telerik.Windows.Controls.ItemContainerGenerator;
     
        if(generator != null && generator.Status == Telerik.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
        {
            foreach (var item in this._linTileView.Items)
            {
                RadTileViewItem _tileItem = generator.ContainerFromItem(item) as RadTileViewItem;
     
                if (_tileItem != null)
                {
                    if (_tileItem.TileState == TileViewItemState.Maximized)
                    {
                        _linID = ((LIN)_tileItem.Header).ID;
                        ViewModel.LoadData(_linID);
     
                        foreach (RadGridView _radGrid in VisualTreeEnumeration.FindVisualChildren<RadGridView>(_tileItem))
                        {
                            RadContextMenu.SetContextMenu(_radGrid, _conditionContextMenu);
                        }
     
                        foreach (RadButton _radButtons in VisualTreeEnumeration.FindVisualChildren<RadButton>(_tileItem))
                        {
                            if (_radButtons.Name == "_btnNew") _radButtons.Command = ViewModel.AddCommand;
                            if (_radButtons.Name == "_btnEdit") _radButtons.Command = ViewModel.EditCommand;
                            if (_radButtons.Name == "_btnDelete") _radButtons.Command = ViewModel.DeleteCommand;
                        }
                    }
                }
            }      
        }
    }
     
    . . .
    If you need further assistance please feel free to ask.

    Greetings,
    Zarko
    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
  7. UGH!!
    UGH!! avatar
    21 posts
    Member since:
    Nov 2010

    Posted 30 Jun 2011 Link to this post

    Unfortunately this isn't working either ... :(  This is always false.  Looks like generator is never null, and it does look like both generator.status && sender are "generatingcontainers".

     

    if(generator != null && generator.Status == Telerik.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)

    And TileState is "restored" and never in Maximized state, so none of the commands fire.
  8. Zarko
    Admin
    Zarko avatar
    755 posts

    Posted 05 Jul 2011 Link to this post

    Hello Ugh!!,
    Could you please examine the attached project and see if this is what you want? And also I'd like to ask you why don't you put everything you want in the DataTemplate with something like this:
    <telerik:RadTileView.ContentTemplate>
        <DataTemplate>
            <Grid>
                <telerik:RadButton VerticalAlignment="Top"
                                               Command="{Binding MyCommand}"
                                               Content="Test" />
                <telerik:RadGridView HorizontalAlignment="Stretch"
                                                 VerticalAlignment="Center"
                                                 AutoGenerateColumns="True"
                                                 ItemsSource="{Binding Products}">
                    <telerik:RadContextMenu.ContextMenu>
                        <telerik:RadContextMenu>
                            <telerik:RadMenuItem Header="Add" />
                            <telerik:RadMenuItem Header="Delete" />
                            <telerik:RadMenuItem Header="Do something else" />
                        </telerik:RadContextMenu>
                    </telerik:RadContextMenu.ContextMenu>
                </telerik:RadGridView>
            </Grid>
        </DataTemplate>
    </telerik:RadTileView.ContentTemplate>
    and don't use any code behind?

    All the best,
    Zarko
    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
Back to Top
DevCraft banner