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!!
6 Answers, 1 is accepted
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
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;
}
}
}
}
}
Any thoughts??
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;
}
}
}
}
}
}
. . .
Greetings,
Zarko
the Telerik team
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.
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
>
All the best,
Zarko
the Telerik team