2016-07-13 87 views
0

我無法將視圖正確綁定到ViewModel。當它顯示時,它只顯示ViewModel的字符串版本。我看過:Setting Window.Content to ViewModel - simple data template not working。但鏈接不再可用。視圖與ViewModel的綁定不正確

我試圖使用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 // ommitted的清晰度。這正確地指向視圖模型。它的觀點和視圖模型不是

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中,它會正確顯示...

謝謝!!

UPDATE: 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; 
     } 
    } 
+0

您的主視圖模型是否顯示屬性是TestViewModels的可觀察集合? – leetibbett

+0

向我們展示MainWindowViewModel屬性 –

+0

您在哪裏爲View設置或分配DataContext? –

回答

0

在視圖有一個數據上下文中聲明。這令人困惑,它看起來像綁定。一旦視圖中的數據上下文被移除並且MainWindowResourses保留了數據上下文,視圖就會按照它應該顯示的那樣顯示。

相關問題