视图未正确绑定到ViewModel

时间:2016-07-13 16:05:08

标签: wpf xaml mvvm binding

我无法让View正确绑定到ViewModel。显示时,它仅显示ViewModel的字符串版本。

我见过:Setting Window.Content to ViewModel - simple data template not working。但该链接不再可用。

我正在尝试使用https://msdn.microsoft.com/en-us/magazine/dd419663.aspx作为模板。

MainWindow.xaml

<Window x:Class="DemoApp.MainWindow"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:vm="clr-namespace:DemoApp.ViewModel"
 xmlns:vw="clr-namespace:DemoApp.View">

<Window.Resources>
    <DataTemplate DataType="{x:Type vm:TestViewModel}">
       <vw:TestView/>
    </DataTemplate>
    <DataTemplate x:Key="ClosableTabItemTemplate">
       <DockPanel Width="120">
         <Button 
            Command="{Binding Path=CloseCommand}"
            Content="X"
            Cursor="Hand"
            DockPanel.Dock="Right"
            VerticalContentAlignment="Bottom"
            Width="16" Height="16"/>
         <ContentPresenter 
            Content="{Binding Path=DisplayName}" 
            VerticalAlignment="Center"/>
       </DockPanel>
      </DataTemplate>
      <DataTemplate x:Key="WorkspacesTemplate">
        <TabControl 
            IsSynchronizedWithCurrentItem="True" 
            ItemsSource="{Binding}" 
            ItemTemplate="{StaticResource ClosableTabItemTemplate}"
            Margin="4" />
    </DataTemplate>
 </Window.Resources>

   <DockPanel>
     <Border
        Grid.Column="2" 
        Style="{StaticResource MainBorderStyle}">
        <HeaderedContentControl 
        Content="{Binding Path=Workspaces}"
        ContentTemplate="{StaticResource WorkspacesTemplate}"
        Header="Workspaces"
        Style="{StaticResource MainHCCStyle}" />
     </Border>
   </DockPanel>
</Window>

MainWindowViewModel.cs //为了清楚起见,省略了。这正确地指向视图模型。它是View和ViewModel之间的绑定,而不是

TestView.xaml

public class TestViewModel : WorkspaceViewModel, INotifyPropertyChanged, 
{
   public Model.Test _test;
   public string DisplayName {get; set;}
   public class TestViewModel(Model.Test t)
   {
      DisplayName = "Test Display Name";
      _model = t;
   }
   // INofifyPropertyChanged Members removed for clarity
}

test.cs中

public class Test
{
    public string FirstName {get; set;}
    public string LastName {get; set;}

    public static DisplayTest()
    {
       return new Test();
    }

}

显示器: DemoApp.ViewModel.TestViewModel;

但是,当我转到MainWindow.xaml并实际输入DockPanel时,它会正确显示...

谢谢!

更新: MainWindowViewModel.cs属性

public ReadOnlyCollection<CommandViewModel> Commands
    {
        get
        {
            if (_commands == null)
            {
                List<CommandViewModel> cmds = this.CreateCommands();
                _commands = new ReadOnlyCollection<CommandViewModel>(cmds);
            }
            return _commands;
        }
    }

public ObservableCollection<WorkspaceViewModel> Workspaces
    {
        get
        {
            if (_workspaces == null)
            {
                _workspaces = new ObservableCollection<WorkspaceViewModel>();
                _workspaces.CollectionChanged += this.OnWorkspacesChanged;
            }
            return _workspaces;
        }
    }

1 个答案:

答案 0 :(得分:0)

在视图中声明了数据上下文。这看起来很混乱。删除视图中的数据上下文并且MainWindowResourses保留数据上下文后,视图将按原样显示。

相关问题