WPF 动态菜单(部分待完成)

WPF 动态菜单

Xaml代码:

<Window x:Class="WPF0322.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="725">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="250"></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <ItemsControl ItemsSource="{Binding MenuModels}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel></StackPanel>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Content="{Binding Name}" Command="{Binding OpenCommand}"></Button>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>
</Window>

Module类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WPF0322
{
    class Module
    {
        public string Name { get; set; }
    }
}

NotificationObject类:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WPF0322
{
    class NotificationObject:INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void RaisePropertyChanged(string propertyName)
        {
            if(this.PropertyChanged != null)
            {
                this.PropertyChanged.Invoke(this,new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

MainViewModule类:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WPF0322
{
    class MainViewModule:NotificationObject
    {
        private List<Module> menuModels;

        public List<Module> MenuModels
        {
            get { return menuModels; }
            set
            {
                menuModels = value;
                this.RaisePropertyChanged("MenuModels");
            }
        }
        public MainViewModule()
        {
            MenuModels = new List<Module>();
            MenuModels.Add(new Module() { Name = "Test1" });
            MenuModels.Add(new Module() { Name = "Test2" });
            MenuModels.Add(new Module() { Name = "Test3" });
        }
    }
}

你可能感兴趣的:(WPF)