(精华)2020年02月04日 WPF课程管理系统项目实战(平台布局-Tab切换页面)

97 阅读1分钟

页面代码

<StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Center">
                    <RadioButton Content="首页" Style="{StaticResource NavButtonStyle}" IsChecked="True"
                                 Command="{Binding NavChangedCommand}"
                                 CommandParameter="FirstPageView"/>
                    <RadioButton Content="关于我们" Style="{StaticResource NavButtonStyle}"/>
                    <RadioButton Content="课程中心" Style="{StaticResource NavButtonStyle}"
                                 Command="{Binding NavChangedCommand}"
                                 CommandParameter="CoursePageView"/>
                    <RadioButton Content="诚聘英才" Style="{StaticResource NavButtonStyle}"/>
                    <RadioButton Content="个人中心" Style="{StaticResource NavButtonStyle}"/>
                </StackPanel>

                <TextBox Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Right" Width="300" Height="32"
                         Template="{StaticResource SearchTextBoxTemplate}" Text="{Binding SearchText}" Foreground="White"/>


                <ContentControl Grid.Row="2" Content="{Binding MainContent}"/>

后台代码

public class MainViewModel : NotifyBase
    {
        public UserModel UserInfo { get; set; }

        private string _searchText;

        public string SearchText
        {
            get { return _searchText; }
            set { _searchText = value; this.DoNotify(); }
        }

        private FrameworkElement _mainContent;

        public FrameworkElement MainContent
        {
            get { return _mainContent; }
            set { _mainContent = value; this.DoNotify(); }
        }

        public CommandBase NavChangedCommand { get; set; }

        public MainViewModel()
        {
            UserInfo = new UserModel();
            this.NavChangedCommand = new CommandBase();
            this.NavChangedCommand.DoExecute = new Action<object>(DoNavChanged);
            this.NavChangedCommand.DoCanExecute = new Func<object, bool>((o) => true);

            DoNavChanged("FirstPageView");
        }

        private void DoNavChanged(object obj)
        {
            Type type = Type.GetType("命名空间." + obj.ToString());
            ConstructorInfo cti = type.GetConstructor(System.Type.EmptyTypes);
            this.MainContent = (FrameworkElement)cti.Invoke(null);
        }
    }