0

我正在开发一个 WPF 项目,以下是我的 Menu 数据模型的代码

class NavigationMenu
    {
        public abstract class MenuBase
        {
            public MenuBase()
            {
            }

            public MenuBase(string title)
            {
                MenuTitle = title;
            }

            public Guid MenuId { get; set; }
            public string MenuTitle { get; set; }
            public string ImageResourceName { get; set; }
        }

        public class MenuItem : MenuBase
        {
            public MenuItem()
            {

            }

            public MenuItem(string title)
                : base(title)
            {

            }

            public string View { get; set; }
        }

        public class MenuCategory : MenuBase
        {
            public MenuCategory()
            {
                MenuItems = new ObservableCollection<MenuItem>();
            }

            public MenuCategory(string title)
                : base(title)
            {
                MenuItems = new ObservableCollection<MenuItem>();
            }

            public MenuCategory(string CategoryTitle, ObservableCollection<MenuItem> pMenuItems)
                : base(CategoryTitle)
            {
                this.MenuItems = pMenuItems;
            }

            public ObservableCollection<MenuItem> MenuItems { get; set; }
        }

        public class MainMenuItem : MenuBase
        {
            public MainMenuItem()
            {
                MenuCategories = new ObservableCollection<MenuCategory>();
            }

            public MainMenuItem(string title)
                : base(title)
            {
                MenuCategories = new ObservableCollection<MenuCategory>();
            }
            public ObservableCollection<MenuCategory> MenuCategories { get; set; }
        }
    }

XAMLRadMenu如下,

<ui:RadMenu Name="radMenu1" ItemsSource="{Binding MenuBar}" Height="76">

        <ui:RadMenu.ItemTemplate>

            <HierarchicalDataTemplate ItemsSource="{Binding Path=MenuCategories}">
                <StackPanel Height="Auto">
                    <Image MaxWidth="32" MaxHeight="32" Source="{Binding Path=ImageResourceName}" />
                    <TextBlock HorizontalAlignment="Center"> 
                        <TextBlock.Text>
                            <MultiBinding  Converter="{StaticResource myConv}">
                                <Binding Path="MenuId"></Binding>
                                <Binding Path="MenuTitle"></Binding>
                             </MultiBinding>
                        </TextBlock.Text>
                   </TextBlock>
                </StackPanel>
            </HierarchicalDataTemplate>
        </ui:RadMenu.ItemTemplate>
      </ui:RadMenu>

我遇到的问题是,MenuCategories 显示成功,但MenuItems内部MenuCategory没有显示。很明显,我没有绑定MenuItems但是MenuCategories. 但我不确定如何MenuItems在上面绑定DataTemplate?任何想法?

4

1 回答 1

0

将模型类绑定到数据模板

    <HierarchicalDataTemplate DataType="{x:Type NavigationMenu.MenuCategory }" ItemsSource="{Binding Path=MenuItems }">
            <StackPanel Height="Auto">
                <Image MaxWidth="32" MaxHeight="32" Source="{Binding Path=ImageResourceName}" />
                <TextBlock HorizontalAlignment="Center"> 
                    <TextBlock.Text>
                        <MultiBinding  Converter="{StaticResource myConv}">
                            <Binding Path="MenuId"></Binding>
                            <Binding Path="MenuTitle"></Binding>
                         </MultiBinding>
                    </TextBlock.Text>
               </TextBlock>
            </StackPanel>
    </HierarchicalDataTemplate>


         <HierarchicalDataTemplate DataType="{x:Type NavigationMenu.MenuItem }">
            <StackPanel Height="Auto">
                //....
                <TextBlock HorizontalAlignment="Center" Text="{Binding View}"/> 
                //....
            </StackPanel>
    </HierarchicalDataTemplate>
于 2012-07-16T10:58:39.630 回答