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

Problem faced while inheriting RadMenuItem class

4 Answers 70 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Rakesh
Top achievements
Rank 1
Rakesh asked on 01 Nov 2008, 12:31 AM
I had written my own node class and it was displaying properly with in RadMenu. I wanted few more features which were provided by RadMenuItem class e.g. StaysOpenOnClick. So I inherited my Node class from RadMenuItem Class. Now the menu stopped displaying.

Could someone point out where I am going wrong here?

4 Answers, 1 is accepted

Sort by
0
Valentin.Stoychev
Telerik team
answered on 01 Nov 2008, 02:33 PM
Hello Rakesh,

Are you assigning the DefaultStyleKey property in your control?

Basically each new controls should specifu what theme resource should be used for rendering. Something similiar to:

        public RadMenuNew()  
        {  
 
            DefaultStyleKey = typeof(RadMenu);  
...  
 

Let us know how it goes.

All the best,
Valentin.Stoychev
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Rakesh
Top achievements
Rank 1
answered on 01 Nov 2008, 04:16 PM
I tried but it doesn't work

see the code below
XAML:

<

UserControl.Resources>

 

 

<Custom:ListOfNodes x:Key="MyList"/>

 

 

<Custom:TaskListDataTemplateSelector x:Key="myDataTemplateSelector"/>

 

 

<Telerik:HierarchicalDataTemplate x:Key="Node" ItemsSource="{Binding Children}">

 

 

<TextBlock Text="{Binding Name}" Foreground="Green"/>

 

 

</Telerik:HierarchicalDataTemplate>

 

 

<Telerik:HierarchicalDataTemplate x:Key="SelectionNode" ItemsSource="{Binding Children}">

 

 

<Grid Width="240">

 

 

<Grid.ColumnDefinitions>

 

 

<ColumnDefinition x:Name="SelColumn1" Width="100"></ColumnDefinition>

 

 

<ColumnDefinition x:Name="SelColumn2" Width="140"></ColumnDefinition>

 

 

</Grid.ColumnDefinitions>

 

 

<TextBlock x:Name="SelName" Foreground="Green" Text="{Binding Name}" Grid.Column="0" Grid.ColumnSpan="1"></TextBlock>

 

 

<ComboBox x:Name="selList" SelectedItem="{Binding SelectedData}" Grid.Column="1" Grid.ColumnSpan="1" Margin="20,0,20,0"></ComboBox>

 

 

</Grid>

 

 

</Telerik:HierarchicalDataTemplate>

 

 

<Telerik:HierarchicalDataTemplate x:Key="TextNode" ItemsSource="{Binding Children}">

 

 

<Grid Width="240">

 

 

<Grid.ColumnDefinitions>

 

 

<ColumnDefinition x:Name="TxtColumn1" Width="100"></ColumnDefinition>

 

 

<ColumnDefinition x:Name="TxtColumn2" Width="140"></ColumnDefinition>

 

 

</Grid.ColumnDefinitions>

 

 

<TextBlock x:Name="txtName" Text="{Binding Name}" Width="50" Grid.Column="0" Grid.ColumnSpan="1"></TextBlock>

 

 

<TextBox x:Name="txtData" Text="{Binding EnteredData}" Grid.Column="1" Grid.ColumnSpan="1" Margin="20,0,20,0"></TextBox>

 

 

</Grid>

 

 

</Telerik:HierarchicalDataTemplate>

 

 

</UserControl.Resources>

 

 

<Grid x:Name="LayoutGrid" Background="White">

 

 

<Grid.RowDefinitions>

 

 

<RowDefinition x:Name="Row1" Height="30"></RowDefinition>

 

 

<RowDefinition x:Name="Row2" Height="250"></RowDefinition>

 

 

<RowDefinition x:Name="Row3"></RowDefinition>

 

 

</Grid.RowDefinitions>

 

 

<Canvas x:Name="MenuCanvas" Grid.Row="0" Grid.RowSpan="1">

 

 

<TelerikNav:RadMenu x:Name="Menu1" ItemsSource="{Binding Source={StaticResource MyList}}" ItemTemplateSelector="{StaticResource myDataTemplateSelector}"></TelerikNav:RadMenu>

 

 

</Canvas>

 

</

Grid>

 

</

UserControl>

CODE:

 

 

public partial class Page : UserControl

 

 

 

{

 

public Page()

 

{

InitializeComponent();

}

}

 

public class Node : RadMenuItem

 

 

 

{

 

protected Node _parent;

 

 

protected ObservableCollection<Node> _children;

 

 

protected string _name;

 

 

 

public Node(string name):base()

 

{

DefaultStyleKey =

typeof(RadMenuItem);

 

_name = name;

_children =

new ObservableCollection<Node>();

 

_children.CollectionChanged +=

new System.Collections.Specialized.NotifyCollectionChangedEventHandler(_children_CollectionChanged);

 

}

 

public Node Parent

 

{

 

get

 

 

 

{

 

return _parent;

 

}

 

set

 

 

 

{

_parent =

value;

 

}

}

 

public ObservableCollection<Node> Children

 

{

 

get

 

 

 

{

 

return _children;

 

}

}

 

void _children_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)

 

{

 

foreach (Node node in e.NewItems)

 

{

node.Parent =

this;

 

}

}

 

public string Name

 

{

 

get

 

{

 

return _name;

 

}

}

}

 

public class SelectionNode : Node

 

 

 

{

 

private List<object> _selectionData;

 

 

private object _selectedData;

 

 

public SelectionNode(string name)

 

:

base(name)

 

{

_selectionData =

new List<object>();

 

}

 

public List<object> SelectionData

 

{

 

get { return _selectionData; }

 

 

set { _selectionData = value; }

 

}

 

public object SelectedData

 

{

 

get { return _selectedData; }

 

 

set { _selectedData = value; }

 

}

}

 

public class TextNode : Node

 

 

 

{

 

private string _enteredData;

 

 

public TextNode(string name)

 

:

base(name)

 

{

 

}

 

public string EnteredData

 

{

 

get

 

 

 

{

 

return _enteredData;

 

}

 

set

 

 

 

{

_enteredData =

value;

 

}

}

}

 

public class TaskListDataTemplateSelector : DataTemplateSelector

 

 

 

{

 

HierarchicalDataTemplate nodeTemplate;

 

 

HierarchicalDataTemplate selectionNodeTemplate;

 

 

HierarchicalDataTemplate textNodeTemplate;

 

 

public TaskListDataTemplateSelector()

 

{

}

 

public override DataTemplate SelectTemplate(object item, DependencyObject container)

 

{

 

FrameworkElement page = Application.Current.RootVisual as FrameworkElement;

 

nodeTemplate = page.Resources[

"Node"] as HierarchicalDataTemplate;

 

selectionNodeTemplate = page.Resources[

"SelectionNode"] as HierarchicalDataTemplate;

 

textNodeTemplate = page.Resources[

"TextNode"] as HierarchicalDataTemplate;

 

 

 

if (item.GetType()== typeof(Node))

 

{

 

return nodeTemplate;

 

}

 

if (item.GetType() == typeof(SelectionNode))

 

{

 

return selectionNodeTemplate;

 

}

 

if (item.GetType() == typeof(TextNode))

 

{

 

return textNodeTemplate;

 

}

 

 

return null;

 

}

}

 

 

public class ListOfNodes : ObservableCollection<Node>

 

{

 

public ListOfNodes()

 

{

 

Node MasterNode = new Node("Master Node");

 

 

Node node1 = new Node("Node1");

 

 

Node nodeA = new Node("NodeA");

 

 

Node nodeI = new Node("NodeI");

 

 

Node nodeII = new Node("NodeII");

 

 

Node nodeIII = new Node("NodeIII");

 

nodeA.Children.Add(nodeI);

nodeA.Children.Add(nodeII);

nodeA.Children.Add(nodeIII);

 

Node nodeB = new SelectionNode("NodeB");

 

 

Node nodeC = new Node("NodeC");

 

 

Node nodeD = new Node("NodeD");

 

node1.Children.Add(nodeA);

node1.Children.Add(nodeB);

node1.Children.Add(nodeC);

node1.Children.Add(nodeD);

 

Node node2 = new Node("Node2");

 

 

Node node3 = new Node("Node3");

 

 

Node node4 = new Node("Node4");

 

 

Node node5 = new Node("Node5");

 

 

Node nodeX = new TextNode("NodeX");

 

 

Node nodeY = new Node("NodeY");

 

 

Node nodeZ = new Node("NodeZ");

 

node5.Children.Add(nodeX);

node5.Children.Add(nodeY);

node5.Children.Add(nodeZ);

MasterNode.Children.Add(node1);

MasterNode.Children.Add(node2);

MasterNode.Children.Add(node3);

MasterNode.Children.Add(node4);

MasterNode.Children.Add(node5);

Add(MasterNode);

 

}

}

0
Rakesh
Top achievements
Rank 1
answered on 01 Nov 2008, 05:37 PM
Think I should rephrase the question. How do I apply RadMenuItem Properties on RadMenu whose ItemsSource is set to a custom datatype.
0
Hristo
Telerik team
answered on 05 Nov 2008, 09:31 AM
Hello Rakesh,

Sorry for the late response.
About your question - you don't need to have Children collection with child nodes. RadMenuItem is ItemsControl and it already have such collection - Items. You should use it or at least populate it so that items can be seen. Also Name and Parent properties come from FrameworkElement base class so there is no need to create your own. Please keep in mind that you can set the Name property in XAML only (with x:Name attribute). You should create properties with other names.

Also one important note. If you extend RadMenuItem class you cannot use ItemTemplate or ItemTemplateSelector. They are used only when you have data items (not UIElement) in your Items collection.
So for your example to work you need to set the name to Header property, remove Children collection and use Items instead and change the Template of RadMenuItem class to include bound ComboBoxes. Or use data object (Node class that do not inherit from RadMenuItem) and use HierarchicalDataTemplates.

Another thing - there is no need to set DefaultStyleKey to the same value (DefaultStyleKey=(RadMenuItem)) in the inherited classes. You can change it if you want to define the Template in the inherited assembly resources.

If you need more help, I'll be glad to help.

Regards,
Hristo
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Menu
Asked by
Rakesh
Top achievements
Rank 1
Answers by
Valentin.Stoychev
Telerik team
Rakesh
Top achievements
Rank 1
Hristo
Telerik team
Share this question
or