WPF中基于Caliburn.Micro框架的数据绑定完整版,包括常用的各个控件和基本操作

2016-10-20 20:08:29来源:CSDN作者:qq_32493189人点击

第七城市

1.新建一个项目,将caliburn.micro引入
2.新建一个AppBootstrapper.cs类

namespace Calub{    public class AppBootstrapper:BootstrapperBase    {        public AppBootstrapper()        {            InitializeViewLocator();        }        private void InitializeViewLocator()        {            base.Initialize();        }        protected override void OnStartup(object sender, System.Windows.StartupEventArgs e)        {            MainWindow main = new MainWindow();            MainViewModel mainM=new MainViewModel();            StudentView stuV = new StudentView();            StudentViewModel stuM = new StudentViewModel();            ViewModelBinder.Bind(stuM, stuV, null);            ViewModelBinder.Bind(mainM, main, null);            main.Show();        }    }}

3.修改App.xaml

<Application x:Class="Calub.App"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             xmlns:local="clr-namespace:Calub"             ><!--StartupUri="MainWindow.xaml"-->    <Application.Resources>        <ResourceDictionary>            <local:AppBootstrapper x:Key="bootstrapper"/>        </ResourceDictionary>    </Application.Resources></Application>

4.在App.xaml.cs里面添加代码

 public partial class App : Application    {        private AppBootstrapper bootStrapper;        public App()        {            bootStrapper = new AppBootstrapper();        }    }

5.新建两个文件夹View和ViewModel,分别添加View.xaml和ViewModel.cs,Property.cs文件,然后在AppBootstrapper类中将两者绑定

6.以下是部分Xaml代码,包括一些控件的动态生成和加载,还有相关事件的绑定

<UserControl x:Class="Calub.View.StudentView"             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"              xmlns:cal="http://www.caliburnproject.org"              xmlns:vm="clr-namespace:Calub.ViewModel"             mc:Ignorable="d"              d:DesignHeight="300" d:DesignWidth="350">    <Grid>        <DockPanel>            <StackPanel DockPanel.Dock="Top"  Orientation="Horizontal">                <TextBox Width="100" Height="25" Text="{Binding Count}"></TextBox>                <TextBox Width="100" Height="25" Text="{Binding Name}"></TextBox>                <Button Width="100" Height="25" Content="添加成员" cal:Message.Attach="[Event Click]=[Action myclick]"></Button>                <ComboBox Width="90" ItemsSource="{Binding ChannlList}" SelectedItem="{Binding ChannlSelect}"></ComboBox>            </StackPanel>             <StackPanel DockPanel.Dock="Top">                <TextBlock Text="{Binding StudentSelect.Name}"></TextBlock>            </StackPanel>            <StackPanel DockPanel.Dock="top" >                <GroupBox Header="按钮">                    <!--动态生成一组按钮-->                    <StackPanel>                        <ItemsControl ItemsSource="{Binding Picture}">                            <ItemsControl.ItemsPanel>                                <ItemsPanelTemplate>                                    <StackPanel Orientation="Horizontal"/>                                </ItemsPanelTemplate>                            </ItemsControl.ItemsPanel>                            <ItemsControl.ItemTemplate>                                <DataTemplate>                                    <RadioButton Content="{Binding Name}" IsChecked="{Binding IsSelect,Mode=TwoWay}" TabIndex="{Binding PIdex}" GroupName="pic"  cal:Message.Attach="[Event Checked]=[Action PictureChecked($view,$eventArgs)]"></RadioButton>                                </DataTemplate>                            </ItemsControl.ItemTemplate>                        </ItemsControl>                    </StackPanel>                </GroupBox>             </StackPanel>            <StackPanel DockPanel.Dock="top">                <ScrollViewer VerticalScrollBarVisibility="Auto" >                    <StackPanel >                            <ListBox ItemsSource="{Binding StudentList}" SelectedItem="{Binding StudentSelect}" BorderThickness="1" Height="180">                                <ListBox.ItemTemplate >                                    <DataTemplate>                                        <StackPanel  Orientation="Horizontal">                                            <TextBlock  Text="{Binding Id}" Margin="5" />                                            <TextBlock Text="{Binding Age}" Margin="5"/>                                            <TextBlock  Text="{Binding Name}" Margin="5"/>                                        </StackPanel>                                    </DataTemplate>                                </ListBox.ItemTemplate>                            </ListBox>                        </StackPanel>                </ScrollViewer>            </StackPanel>            <StackPanel  DockPanel.Dock="top">                <ScrollViewer   VerticalScrollBarVisibility="Visible" >                    <ItemsControl ItemsSource="{Binding PersonList}">                        <ItemsControl.ItemsPanel>                            <ItemsPanelTemplate>                                <WrapPanel></WrapPanel>                            </ItemsPanelTemplate>                        </ItemsControl.ItemsPanel>                        <ItemsControl.ItemTemplate>                            <DataTemplate>                                <StackPanel Orientation="Horizontal" Margin="10">                                    <TextBlock Text="{Binding Ip}"></TextBlock>                                    <TextBlock Text="{Binding Port}"></TextBlock>                                    <TextBlock Text="{Binding Name}"></TextBlock>                                </StackPanel>                            </DataTemplate>                        </ItemsControl.ItemTemplate>                    </ItemsControl>                </ScrollViewer>            </StackPanel>        </DockPanel>      </Grid></UserControl>

7.部分CS代码,包括文件的保存和读取

//Property.csnamespace Calub.ViewModel{    public class StudentProperty:PropertyChangedBase    {        private int count = 50;        public int Count        {            get { return count; }            set            {                count = value;                NotifyOfPropertyChange(() => Count);            }        }        private ObservableCollection<Student> studentList = new ObservableCollection<Student>();        public ObservableCollection<Student> StudentList        {            get { return studentList; }            set            {                studentList = value;                NotifyOfPropertyChange(() => StudentList);            }        }        private Student studentSelect;        public Student StudentSelect        {            get { return studentSelect; }            set            {                studentSelect = value;                NotifyOfPropertyChange(() => StudentSelect);            }        }        private ObservableCollection<string> channlList = new ObservableCollection<string>();        public ObservableCollection<string> ChannlList        {            get { return channlList; }            set            {                channlList = value;                NotifyOfPropertyChange(() => ChannlList);            }        }        private string channlSelect;        public string ChannlSelect        {            get { return channlSelect; }            set            {                channlSelect = value;                NotifyOfPropertyChange(() => ChannlSelect);            }        }        private int id=1;        public int Id        {            get { return id; }            set            {                id = value;                NotifyOfPropertyChange(() => Id);            }        }        private int age=22;        public int Age        {            get { return age; }            set            {                age = value;                NotifyOfPropertyChange(() => Age);            }        }        private string name="spad";        public string Name        {            get { return name; }            set            {                name = value;                NotifyOfPropertyChange(() => Name);            }        }        private ObservableCollection<PictureProperty> picture = new ObservableCollection<PictureProperty>();        public ObservableCollection<PictureProperty> Picture        {            get { return picture; }            set            {                picture = value;                NotifyOfPropertyChange(() => Picture);            }        }        private ObservableCollection<PersonProperty> personList = new ObservableCollection<PersonProperty>();        public ObservableCollection<PersonProperty> PersonList        {            get { return personList; }            set            {                personList = value;                NotifyOfPropertyChange(() => PersonList);            }        }    }}
//ViewModel.csnamespace Calub.ViewModel{    public class StudentViewModel:StudentProperty    {        public StudentViewModel()        {            InitPicture();            InitPerson(0);        }        public void myclick()        {            StudentList.Add(new Student {Id=Id,Age=Age,Name=Name });            ChannlList.Add(Id+Name+Age);            Id++;        }        public void InitPicture()        {            string[] name={"1","2","3","4"};            for (int i = 0; i < 4; i++)            {                PictureProperty pic = new PictureProperty();                pic.Name = name[i];                pic.PIdex = i;                Picture.Add(pic);            }            Picture[0].IsSelect = true;        }        public void InitPerson(int num)        {            PersonList.Clear();            for (int i = 0; i < num; i++)            {                PersonProperty per = new PersonProperty();                if (StudentList.Count > i)                    per.Name = StudentList[i].Name;                else                {                    per.Name = null;                }                PersonList.Add(per);            }        }        public void PictureChecked(object sender,RoutedEventArgs e){            if(e.Source is RadioButton){                RadioButton rb=(RadioButton)e.Source;                int num = rb.TabIndex + 1;                InitPerson(num*5);            }        }    }}
第七城市

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台