繁体   English   中英

命令没有显示正确的用户控件

[英]Command does not show the right usercontrol

我有一个WPF应用程序,如下所示: 在此处输入图片说明 当用户单击左侧(导航栏)(例如服务器按钮)时,它将显示给服务器usercontrol。 它应该在右侧显示不同的屏幕,具体取决于用户单击了哪个按钮。 xaml的主要文件代码:

<igWpf:XamRibbonWindow x:Class="BackupCustomizing.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:ignore="http://www.ignore.com"
        xmlns:ig="http://schemas.infragistics.com/xaml"
        xmlns:views="clr-namespace:BackupCustomizing.Views"
        xmlns:viewmodel="clr-namespace:BackupCustomizing.ViewModel"               
        xmlns:igWpf="http://schemas.infragistics.com/xaml/wpf"
        mc:Ignorable="d ignore"
        Height="400"
        Width="700"
        Title="Backup customizing V0.1"
        DataContext="{Binding Main, Source={StaticResource Locator}}" ResizeMode="NoResize">

    <igWpf:XamRibbonWindow.Resources>
        <DataTemplate DataType="{x:Type viewmodel:ServerViewModel}">
            <views:ServerView/>
        </DataTemplate>
    </igWpf:XamRibbonWindow.Resources>

    <ig:ThemeManager.Theme>
        <ig:Office2013Theme />
    </ig:ThemeManager.Theme>
    <igWpf:RibbonWindowContentHost x:Name="_content"
                                   Theme="Office2013"
                                   igWpf:RibbonWindowContentHost.ApplicationAccentColor="#0072C6">
        <Grid x:Name="LayoutRoot">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <views:NavigationView Grid.Column="0"/>
            <ContentPresenter Content="{Binding CurrentViewModel}" Grid.Column="1"/>
        </Grid>

    </igWpf:RibbonWindowContentHost>
</igWpf:XamRibbonWindow>

如您在上面看到的,我有一个引用数据类型ServerViewModel的数据模板

<igWpf:XamRibbonWindow.Resources>
        <DataTemplate DataType="{x:Type viewmodel:ServerViewModel}">
            <views:ServerView/>
        </DataTemplate>
</igWpf:XamRibbonWindow.Resources>

一个内容演示者应该在右侧显示当前的用户控件,具体取决于按下哪个按钮。

<ContentPresenter Content="{Binding CurrentViewModel}" Grid.Column="1"/>

以及后面的代码:

using System.Diagnostics;
using BackupCustomizing.Model;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;

namespace BackupCustomizing.ViewModel
{
    /// <summary>
    ///     This class contains properties that the main View can data bind to.
    ///     <para>
    ///         See http://www.galasoft.ch/mvvm
    ///     </para>
    /// </summary>
    public class MainViewModel : ViewModelBase
    {
        private readonly IDataService _dataService;
        private ViewModelBase _currentViewModel;
        private ServerViewModel _serverViewModel;

        /// <summary>
        /// Views change commands.
        /// </summary>
        public RelayCommand ServerCmd { get; set; }


        public ViewModelBase CurrentViewModel
        {
            get { return _currentViewModel; }
            set
            {
                _currentViewModel = value;
                RaisePropertyChanged("CurrentViewModel");
            }
        }

        /// <summary>
        ///     Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel(IDataService dataService)
        {
            _dataService = dataService;
            _dataService.GetData(
                (item, error) => { });

            _serverViewModel = new ServerViewModel();
            //_currentViewModel = _serverViewModel;

            ServerCmd = new RelayCommand(_SetServerView);
        }



        public void _SetServerView()
        {
            _currentViewModel = new ServerViewModel();
        }
    }
}

我将serverCmd relaycommand绑定到一个函数:

ServerCmd = new RelayCommand(_SetServerView);

应该显示来自服务器的usercontrol,它将currentview设置为serverview。

public void _SetServerView()
        {
            _currentViewModel = new ServerViewModel();
        }

按钮服务器命令的绑定如下:

<Button Grid.Row="0" Content="Server" Margin="10 10 5 0" Command="{Binding ServerCmd}"/>

问题是,当我单击按钮server ,它为什么不显示服务器的用户控件?
如果我在viewmodel构造函数中使用serverview实例化currentview,例如:

public MainViewModel(IDataService dataService)
        {
            _dataService = dataService;
            _dataService.GetData(
                (item, error) => { });

            _serverViewModel = new ServerViewModel();
            _currentViewModel = _serverViewModel;

            ServerCmd = new RelayCommand(_SetServerView);
        }

然后向我显示服务器的usercontrol: 在此处输入图片说明

ServerCmd问题所在,在ServerCmd命令中,应该将属性而不是字段设置为RaisePropertyChange ,因此请将_SetServerView()更改为:

public void _SetServerView()
{
     CurrentViewModel = new ServerViewModel();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM