Question : Silverlight UserControl with child UserControls

I am trying to set up a Menu control in Silverlight that will allow me to add child MenuItems in my MainPage.xaml.  The page renders properly with the following code, but there are two problems:

1.  The MainPage.xaml designer shows a warning that "Property 'Content' does not support values of type 'MenuItemCollection'"

2.  In the code behind, I cannot access the MenuItem directly, such as miOne.Text = "Three".  When I run the project, miOne always seems to be Nothing.  If the MenuItem is created outside of the Menu parent, it works fine.


Please find the code below.  Any help is very much appreciated.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
MainPage.xaml
----------------------------------------------------------------------------------------------------

  
        
            
                
                
            
        
    

----------------------------------------------------------------------------------------------------
 
Menu.xaml
----------------------------------------------------------------------------------------------------

    
        
            
        
    

----------------------------------------------------------------------------------------------------
 
 
Menu.xaml.vb
----------------------------------------------------------------------------------------------------
 _
Partial Public Class Menu
    Inherits UserControl
    Public Property Items() As MenuItemCollection
        Get
            Return TryCast(Me.GetValue(ItemsProperty), MenuItemCollection)
        End Get
        Set(ByVal value As MenuItemCollection)
            Me.SetValue(ItemsProperty, value)
        End Set
    End Property
 
    Public Sub New()
        InitializeComponent()
    End Sub
 
    Public Shared ItemsProperty As DependencyProperty = DependencyProperty.Register("Items", GetType(MenuItemCollection), GetType(Menu), Nothing)
 
    Private Sub Menu_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        If Me.Items Is Nothing Then
            Me.Items = New MenuItemCollection()
        End If
 
        For Each i As MenuItem In Me.Items
            Me.sp.Children.Add(i)
        Next
    End Sub
End Class
----------------------------------------------------------------------------------------------------
 
 
MenuItemCollection.vb
----------------------------------------------------------------------------------------------------
Public Class MenuItemCollection
    Inherits System.Collections.ObjectModel.ObservableCollection(Of MenuItem)
End Class
----------------------------------------------------------------------------------------------------
 
 
MenuItem.xaml
----------------------------------------------------------------------------------------------------

    
        

Answer : Silverlight UserControl with child UserControls

As your menu is separate user control you can not access those properties.
What I would suggest you to do is create some DependencyPrperties on ContextMenu.Menu item which takes List<String> and  on the call back of that DP changed you create different menu items using those string in the list and place them in the stack panel.

So in your main page what you have to do is just create list of string and assign that as the value to the DP you created on MenuItem control.

Hope it helps

Random Solutions  
 
programming4us programming4us