Dienstag, 7. Juni 2011

Silverlight 5 beta: Issue with Toolkit controls

With Silverlight 5 beta there is an issue concerning the usage of the Silverlight Toolkit controls. For some of them (in my sample the MenuItem) you get a compile error

“The type or namespace name 'ContextMenu' does not exist in the namespace 'System.Windows.Controls' (are you missing an assembly reference?)”

Oops?! If you use intellisense to select the MenuItem control it exists. So definitively a bug in Silverlight 5. After a quick search a found also a hint in Microsoft Connect: Silverlight Forum: SL5 NumericUpDown control compile error | Microsoft Connect

XAML which produce the error:

<StackPanel>
    <ListBox x:Name="ListBoxTest">
        <toolkit:ContextMenuService.ContextMenu>
            <toolkit:ContextMenu x:Name="contextMenu1">
                <toolkit:MenuItem Header="Test"></toolkit:MenuItem>
            </toolkit:ContextMenu>
        </toolkit:ContextMenuService.ContextMenu>
        <ListBoxItem Content="Listitem 1"></ListBoxItem>
        <ListBoxItem Content="Listitem 2"></ListBoxItem>
    </ListBox>
</StackPanel>

Workaround by setting the Menuitems with code behind:

New XAML:

<StackPanel>
    <ListBox x:Name="ListBoxTest">
        <toolkit:ContextMenuService.ContextMenu>
            <toolkit:ContextMenu x:Name="contextMenu1">
                <!--<toolkit:MenuItem Header="Test"></toolkit:MenuItem>-->
            </toolkit:ContextMenu>
        </toolkit:ContextMenuService.ContextMenu>
        <ListBoxItem Content="Listitem 1"></ListBoxItem>
        <ListBoxItem Content="Listitem 2"></ListBoxItem>
    </ListBox>
</StackPanel>

Code:

public MainPage()
{
    InitializeComponent();
 
    var menuitem = new MenuItem() { Header = "My Menu" };
    //menuitem.Click += new RoutedEventHandler(menuitem_Click);
 
    contextMenu1.Items.Add(menuitem);
}

Keine Kommentare:

Kommentar veröffentlichen