WPF:无法访问视图。 Datagrid不会刷新选项卡中的数据

时间:2014-03-10 18:08:15

标签: c# wpf datagrid

我正在关注此How to create tab-able content in WPF/C#?,但我希望每个标签都显示一个数据网格。数据网格不显示,也不显示数据。当我进入代码时,我确实看到0,1被设置。

MainWindow.xaml

<Window x:Class="MVVMDataInstances.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:viewModel="clr-namespace:MVVMDataInstances"
        Title="MainWindow" Height="350" Width="525">

    <Window.DataContext>
        <viewModel:MainWindowViewModel />
    </Window.DataContext>
    <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="ContentTemplate" 
                      DataType="{x:Type viewModel:ChildViewModel}"/>
        </Grid.Resources>
        <TabControl ContentTemplate="{StaticResource ContentTemplate}"  
                ItemsSource="{Binding Items}" />
    </Grid>
</Window>

MainWindowViewModel.cs

 public ObservableCollection<ChildViewModel> Items { get; private set; }

        public MainWindowViewModel()
        {

            Items = new ObservableCollection<ChildViewModel> {new ChildViewModel(0), new ChildViewModel(1)};
        }

ChildView.xaml

<UserControl x:Class="MVVMDataInstances.View"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:ViewModel="clr-namespace:MVVMDataInstances"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.DataContext>
        <ViewModel:ChildViewModel/>
    </UserControl.DataContext>
            <DataGrid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding Grid, Mode=TwoWay}" />

</UserControl>

ChildViewModel.cs

public class ChildViewModel : ViewModelBase
    {
        private ObservableCollection<ChildModel> _grid;

        public ObservableCollection<ChildModel> Grid
        {
            get { return _grid; }
            private set
            {
                _grid = value;
                OnPropertyChanged("Grid");
            }
        }

        public ChildModel Data { get; set; }

        public ChildViewModel()
        {
        }

        public ChildViewModel(int tabNumber)
        {
            Data = new ChildModel {A = tabNumber.ToString(CultureInfo.InvariantCulture)};

            Grid = new ObservableCollection<ChildModel> {Data};
        }
    }

ChildModel.cs

public class ChildModel
    {
        public string A { get; set; }
        public string B { get; set; }
    }

ViewModelBase.cs

 public class ViewModelBase : INotifyPropertyChanged
    {

        public void OnPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(name));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

我希望每个标签看到一个网格。对于属性A,第一个选项卡上的条目的值为0.第二个选项卡的条目对于属性B的值为1.

我看到当调用OnPropertyChanged时,PropertyChanged为null。

如果我在MainWindow.xaml

中有这个,我可以访问数据网格
<Grid>
        <TabControl ItemsSource="{Binding Items}">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding TabTitle}"/>
                </DataTemplate>
            </TabControl.ItemTemplate>
            <TabControl.ContentTemplate>
                <DataTemplate>
                    <DataGrid HorizontalAlignment="Stretch" VerticalAlignment="Stretch" AutoGenerateColumns="True"
                  ItemsSource="{Binding Grid}"/>
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>
    </Grid>

但OnPropertyChanged对此总是为空,我看不到网格

<DataTemplate x:Key="ContentTemplate" 
                  DataType="{x:Type viewModel:ChildViewModel}">
     <viewModel:ChildView /> 
</DataTemplate>

3 个答案:

答案 0 :(得分:0)

您的DataTemplate为空。根据您的问题,您可能希望按照以下方式执行某些操作:

<Grid>
    <Grid.Resources>
        <DataTemplate x:Key="ContentTemplate" TargetType="{x:Type viewModel:ChildViewModel}">
            <DataGrid AutoGenerateColumns="False" . . .>
                <DataGrid.Columns>
                    <!-- Your column definitions here -->
                </DataGrid.Columns>
            </DataGrid>
        </DataTemplate>
    </Grid.Resources>
    <TabControl ContentTemplate="{StaticResource ContentTemplate}"  
            ItemsSource="{Binding Items}" />
</Grid>

答案 1 :(得分:0)

这个DataTemplate告诉ChildViewModel.cs它的外观是View.xaml:

 <DataTemplate x:Key="ContentTemplate" 
                  DataType="{x:Type viewModel:ChildViewModel}">
     <viewModel:View /> 
</DataTemplate>

在幕后,它还将每个View的DataContext设置为ChildViewModel的实例。

答案 2 :(得分:0)

我按照此MVVM: ViewModel inheritance进行ViewModel继承。 PropertyChanged为null的原因是因为构造函数已初始化两次。第一次使用整数,然后第二次使用InitializedComponent。如果我注释掉InitializedComponent,则网格从未初始化。另一个stackoverflow问题的答案使得视图模型继承更加清晰。