2012-05-22 7 views
0

나는 hiarchialdatabinding을 사용하여 관찰 가능한 컬렉션으로 채워진 WPF TreeView를가집니다. 내 관찰 가능 컬렉션 또는 해당 개체를 채우는 데 사용 된 데이터베이스의 항목에 액세스해야합니다. 예제 사용 사례는 사용자가 하위 그룹을 추가하기 위해 treeview 항목을 마우스 오른쪽 버튼으로 클릭한다는 것입니다. 아이를 추가하려면 부모에게 액세스해야합니다. 제안 사항이 있으십니까? 임 그래서 .. 손실항목에서 관찰 가능한 컬렉션에있는 항목에 액세스

난 그냥 트 리뷰 항목 자체가 다음 변경 실 거예요 다시 내 데이터베이스에 반영 원인을 편집하지 못할

데이터베이스 코드 :

[Serializable] 
public class LoginGroup 
{ 
    public string Name { get; set; } 
    public Guid ID { get; set; } 
    public List<Login> LoginItems = new List<Login>(); 
    public List<LoginGroup> Children { get; set; } 
} 

public static ObservableCollection<LoginGroup> _GroupCollection = new ObservableCollection<LoginGroup>(); 

public ObservableCollection<LoginGroup> GroupCollection 
{ 
    get { return _GroupCollection; } 
} 

트 리뷰 :

<TreeView x:Name="groupView" Width="211" TreeViewItem.Selected="OnTreeItemSelected" DockPanel.Dock="Left" Height="Auto" ItemsSource="{Binding GroupCollection}" > 
    <TreeView.ItemTemplate> 
     <HierarchicalDataTemplate ItemsSource="{Binding Path=Children}"> 
      <TextBlock Text="{Binding Path=Name}" /> 
     </HierarchicalDataTemplate> 
    </TreeView.ItemTemplate> 
</TreeView> 

답변

0

SelectedItemLoginGroup으로 전송하면

LoginGroup selectedGroup = (LoginGroup)groupView.SelectedItem; 
+0

을 상속 받아야합니다. 제가 많이 알고있는 것은 제 문제는 아닙니다. 마지막 줄을보십시오. 트리보기 항목 자체를 편집하면 변경 사항이 내 데이터베이스에 반영되지 않습니다. – Lexious

+0

@Lexi, TreeView 자체를 편집하는 것이 좋습니다. 'selectedGroup'에 필요한 변경 사항을 수행 한 다음 데이터베이스에 저장하십시오. 정확히 어느 부분에 문제가 있습니까? –

+0

글쎄, 그건 내 문제 야. .. 내가 어떻게 데이터베이스에있는 그 항목에가는 길을 찾아 다니고, 아이의 아이의 아이를 말하면 좋을지 모르겠다. 데이터베이스의 위치? 필자는 고유 한 식별자를 사용하여 실험했으나 행운이 없었다. – Lexious

0

TreeView의 ItemContainer 스타일을 사용해야합니다.

public class TreeNode : ViewModel 
{ 
    public TreeNode() 
    { 
     this.children = new ObservableCollection<TreeNode>(); 

     // the magic goes here 
     this.addChildCommand = new RelayCommand(obj => AddNewChild()); 
    } 

    private void AddNewChild() 
    { 
     // create new child instance 
     var child = new TreeNode 
     { 
      Name = "This is a new child node.", 
      IsSelected = true // new child should be selected 
     }; 

     // add it to collection 
     children.Add(child); 

     // expand this node, we want to look at the new child node 
     IsExpanded = true; 
    } 

    public String Name 
    { 
     get { return name; } 
     set 
     { 
      if (name != value) 
      { 
       name = value; 
       OnPropertyChanged("Name"); 
      } 
     } 
    } 
    private String name; 

    public Boolean IsSelected 
    { 
     get { return isSelected; } 
     set 
     { 
      if (isSelected != value) 
      { 
       isSelected = value; 
       OnPropertyChanged("IsSelected"); 
      } 
     } 
    } 
    private Boolean isSelected; 

    public Boolean IsExpanded 
    { 
     get { return isExpanded; } 
     set 
     { 
      if (isExpanded != value) 
      { 
       isExpanded = value; 
       OnPropertyChanged("IsExpanded"); 
      } 
     } 
    } 
    private Boolean isExpanded; 

    public ObservableCollection<TreeNode> Children 
    { 
     get { return children; } 
    } 
    private ObservableCollection<TreeNode> children; 

    public ICommand AddChildCommand 
    { 
     get { return addChildCommand; } 
    } 
    private RelayCommand addChildCommand; 
} 

일부 의견 :

  • 의 ViewModel가에서 INotifyPropertyChanged 인터페이스의 기본 구현입니다
    다음은 샘플 TreeNode를보기 모델입니다.
  • RelayCommand (a.k.a DelegateCommand)는 MVVM 접근 방식에서 사용하기위한 ICommand 구현입니다.

다음은 뷰의 :

<Window x:Class="WpfApplication1.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <TreeView ItemsSource="{Binding}"> 
     <TreeView.ItemContainerStyle> 
      <!-- Let's glue our view models with the view! --> 
      <Style TargetType="{x:Type TreeViewItem}"> 
       <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> 
       <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /> 
       <Setter Property="ContextMenu"> 
        <Setter.Value> 
         <ContextMenu> 
          <!-- Here's menu item, which is responsible for adding new child node --> 
          <MenuItem Header="Add child..." Command="{Binding AddChildCommand}" /> 
         </ContextMenu> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </TreeView.ItemContainerStyle> 
     <TreeView.ItemTemplate> 
      <HierarchicalDataTemplate ItemsSource="{Binding Children}"> 
       <TextBlock Text="{Binding Name}"/> 
      </HierarchicalDataTemplate> 
     </TreeView.ItemTemplate> 
    </TreeView> 
</Window> 

... 샘플 데이터 컨텍스트 초기화 :이 도움이

public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = new ObservableCollection<TreeNode> 
     { 
      new TreeNode { Name = "Root", IsSelected = true } 
     }; 
    } 

희망.

업데이트.
물론 하위 노드를 ObservableCollection으로 노출해야합니다. 그렇지 않으면 노드 모음에 대한 변경 사항이 반영되지 않습니다.

+0

실례합니다. 하지만 이처럼 뭔가를 사용하여 항목을 선택했을 때 상황에 맞는 메뉴가 클릭되었을 때 자녀를 추가하는 방법에 대해 확신 할 수 없습니다. – Lexious

+0

@Lexi, ItemContainerStyle에서도 컨텍스트 메뉴를 정의 할 수 있습니다. 항목을 메뉴에 추가하면 항목의 명령이보기 모델의 ICommand 속성에 바인딩됩니다. 그런 다음이 명령을 처리하십시오 (하위 항목을 추가하는 코드 작성). – Dennis

+0

@ Lexi, 나는 더 명확하게 대답을 업데이트 할 것이다. – Dennis

0

속성을 변경 한 것을 다시 확인할 수있는 방법이 없기 때문에 속성을 변경 한 것을 되돌릴 수 없습니다. 상속이 필요합니다. DependencyObject에서 INotifyPropertyChanged

+0

그래서 INotifyPropertyChanged를 구현하고 groupView.SelectedItem을 통해 데이터를 변경하면 자동으로 r 다시 돌아 왔니? – Lexious

+0

'Name'속성을 변경하려는 경우 - 예. – RredCat

+0

자녀를 목록에 추가해야하는 경우 어떻게해야합니까? 그것도 반영 될 것입니다. – Lexious

관련 문제