使用MVVM的Datagrid树视图

时间:2013-03-08 12:50:38

标签: c# wpf mvvm

我对WPF& MVVM。我正在创建一个应用程序,其中我在窗口中有三个窗格。

  1. 显示TreeView - 我从DB获取的所有节点并创建了Hierarchical结构。它的工作性很好。
  2. 第二个窗格显示DataGrid,其中我想显示来自db的一些数据但是对于db查询,我需要从所选树节点输入一个。
  3. 所以我得到了所有内容,但问题是我在DataTable所选节点上TreeViewDataTable绑定到DataGrid我没有将数据输入网格我的网格是空白的。 我不知道我是如何做到这一点的。正如我所见,很多例如在搜索时,每个人只有一个DataContext用于示例应用程序,并且其工作正常。 因此,如果任何人可以提供一些示例代码或应用程序,请求所有人。

    谢谢&的问候,

    这是我的用户控件XAML,我为每个树节点都有单独的视图模型。

    <Grid>
       <Border Height="Auto" Width="Auto">
          <Grid>
             <DockPanel Grid.Column="0">
                <TreeView>
                   <TreeView.ItemContainerStyle>
                      <Style TargetType="{x:Type TreeViewItem}">
                         <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
                         <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
                         <Style.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                               <Setter Property="FontWeight" Value="Bold" />
                            </Trigger>
                         </Style.Triggers>
                      </Style>
                   </TreeView.ItemContainerStyle>
                   <TreeView.Resources>
                      <HierarchicalDataTemplate DataType="{x:Type local:RVM}" ItemsSource="{Binding ChildNode}">
                         <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding RootNodeName}" />
                         </StackPanel>
                      </HierarchicalDataTemplate>
                      <HierarchicalDataTemplate DataType="{x:Type local:PNVM}" ItemsSource="{Binding ChildNode}">
                         <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding ParentNodeName}" />
                         </StackPanel>
                      </HierarchicalDataTemplate>
                      <HierarchicalDataTemplate DataType="{x:Type local:CNVM}" ItemsSource="{Binding ChildNode}">
                         <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding ChildNodeName}" />
                         </StackPanel>
                      </HierarchicalDataTemplate>
                      <DataTemplate DataType="{x:Type local:SubNodeForChildViewModel}">
                         <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding SubNodeForChildName}" />
                         </StackPanel>
                      </DataTemplate>
                   </TreeView.Resources>
                </TreeView>
             </DockPanel>
             <Grid Grid.Column="1">
                <Grid.RowDefinitions>
                   <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <StackPanel Grid.Row="0" Margin="10,0,0,0">
                   <Grid>
                      <DataGrid DataContext="MyDataTable" ItemsSource="{Binding MyDataTable}" />
                   </Grid>
                </StackPanel>
             </Grid>
          </Grid>
       </Border>
    </Grid>
    

    我不知道是否应该为数据网格添加新的视图模型或者如何填充网格。我在这个用户控件中添加了代码我的意思是如果我在代码中添加了选择更改事件然后我可以填充数据网格但我认为我打破了MVVM链......

    与树视图相关联的类

    namespace Tree_Grid_Test.LoadTreeItems
    {    
        public class TreeViewItemViewModel : INotifyPropertyChanged
        {
            static readonly TreeViewItemViewModel dummy = new TreeViewItemViewModel();
    
            ObservableCollection<TreeViewItemViewModel> _ChildNode;
            TreeViewItemViewModel _parent;
    
            bool _isExpanded;
            bool _isSelected;
    
    
            protected TreeViewItemViewModel(TreeViewItemViewModel parent, bool lazyLoadChildNode)
            {
                _parent = parent;
    
                _ChildNode = new ObservableCollection<TreeViewItemViewModel>();           
                _myDataTable = new DataTable();
                if (lazyLoadChildNode)
                    _ChildNode.Add(dummy);
            }
    
    
            private TreeViewItemViewModel()
            {
            }
    
    
    
            /// <summary>
            /// Returns the logical child node items
            /// </summary>
            public ObservableCollection<TreeViewItemViewModel> ChildNode
            {
                get { return _ChildNode; }
            }
    
    
            DataTable _myDataTable;
            public DataTable MyDataTable
            {
                get { return _myDataTable; }
    
                set
                { 
                    if (_myDataTable == value) return;
                    _myDataTable = value;
                    OnPropertyChanged("MyDataTable");
                }
            }
    
    
            public bool HasDummy
            {
                get { return this.ChildNode.Count == 1 && this.ChildNode[0] == dummy; }
            }
    
            public bool IsExpanded
            {
                get { return _isExpanded; }
                set
                {
                    if (value != _isExpanded)
                    {
                        _isExpanded = value;
                        this.OnPropertyChanged("IsExpanded");
                    }                
                    if (_isExpanded && _parent != null)
                        _parent.IsExpanded = true;
                    if (this.HasDummy)
                    {
                        this.ChildNode.Remove(dummy);
                        this.LoadChildNode();
                    }
                }
            }
    
            public bool IsSelected
            {
                get { return _isSelected; }
                set
                {
                    if (value != _isSelected)
                    {
                        _isSelected = value;
                        this.OnPropertyChanged("IsSelected");
                        if (_isSelected)
                        {
                            this.LoadErrorDetails(this);
                            OnPropertyChanged("MyDataTable");                        
                        }
                    }
                }
            }  
    
    
            protected virtual void LoadChildNode() //i override this method in all the viewmodel class and added child node item in 'ChildNode'
            {
            }
    
            protected virtual void LoadDetails(object obj) // I want to override this into all the viewmodel or something to fill the datagrid or better solution
            {
    
            }
    
            public TreeViewItemViewModel Parent
            {
                get { return _parent; }
            }
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected virtual void OnPropertyChanged(string propertyName)
            {
                if (this.PropertyChanged != null)
                    this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }        
        }   
    }
    

    RVS

0 个答案:

没有答案