简体   繁体   English

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

[英]Command does not show the right usercontrol

I have a WPF application that looks like: 我有一个WPF应用程序,如下所示: 在此处输入图片说明 When the user click on the left side(navigation bar) for example the server button, then it should display to server usercontrol. 当用户单击左侧(导航栏)(例如服务器按钮)时,它将显示给服务器usercontrol。 It should show different screen on the right side depends on, which button that the user clicked. 它应该在右侧显示不同的屏幕,具体取决于用户单击了哪个按钮。 The main xaml file code: 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>

As you can see above, I have a datatemplate that reference the datatype ServerViewModel 如您在上面看到的,我有一个引用数据类型ServerViewModel的数据模板

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

An a content presenter that should show the current usercontrol on the right side, depends which button was pressed. 一个内容演示者应该在右侧显示当前的用户控件,具体取决于按下哪个按钮。

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

And the code behind: 以及后面的代码:

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();
        }
    }
}

I bound the serverCmd relaycommand to a function: 我将serverCmd relaycommand绑定到一个函数:

ServerCmd = new RelayCommand(_SetServerView);

that should display usercontrol from server, it set currentview to serverview. 应该显示来自服务器的usercontrol,它将currentview设置为serverview。

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

The button server command is bound as follow: 按钮服务器命令的绑定如下:

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

The problem is, when I clicked on the button server , it does not display the server usercontrol why? 问题是,当我单击按钮server ,它为什么不显示服务器的用户控件?
If I instantiate the currentview with serverview in the viewmodel constructor like: 如果我在viewmodel构造函数中使用serverview实例化currentview,例如:

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

            _serverViewModel = new ServerViewModel();
            _currentViewModel = _serverViewModel;

            ServerCmd = new RelayCommand(_SetServerView);
        }

Then it shows me the server usercontrol: 然后向我显示服务器的usercontrol: 在此处输入图片说明

Spot the problem, in your ServerCmd command you should set the property not the field in order to RaisePropertyChange , so change your _SetServerView() to: ServerCmd问题所在,在ServerCmd命令中,应该将属性而不是字段设置为RaisePropertyChange ,因此请将_SetServerView()更改为:

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

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

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