2017-12-05 42 views
-1

AllowDrop 속성을 가진 ViewItem 모델 클래스가 있습니다. 내 View Model ViewModel은 관찰 가능한 ViewItem 컬렉션입니다.WPF TreeView의 ViewModel에서 AllowDrop 속성 가져 오기

ViewItem 속성

public bool AllowDrop 
    { 
     get 
     { 
      return _allowDrop; 
     } 
    } 

나는 ViewTree이 데이터 소스가 뷰 모델, MyItems의 인스턴스에 바인딩 이잖아.

ViewTreeItems AllowDrop 속성을 기본 모델에 바인딩 할 수는 있지만 액세스 할 수있는 올바른 방법은 알아낼 수 없습니다. TreeView에 대한

내 XAML 내가 AllowDrop의 속성에 바인딩 할 MYVIEW의 컬렉션의 ViewItem에 액세스하는 방법에 부착하고이

 <TreeView x:Name="ViewsTree" 
        AllowDragDrop="True" 
        DragOver="ViewsTree_DragOver" 
        ItemsSource="{Binding MyItems}" 
        ItemTemplate="{StaticResource ViewTemplate}" 
     <Style TargetType="{x:Type TreeViewItem}"> 
      <Setter Property="AllowDrop" Value="{Binding}"/> 
     </Style> 
    </TreeView> 

것 같습니다.

+0

귀하의 질문에 너무 애매합니다. 여기에는 [mcve]가 포함되지 않지만 더 중요한 것은 데이터에서 발견 된 여러 값이 뷰의 _single_ 값에 매핑되어야하는 이유와 이유가 무엇인지 명확하지 않습니다. 모든보기 항목 'AllowDrop' 속성이 같은 값을 가지고 있지 않다면 어떻게 될 것으로 예상됩니까? 무엇을 기대하든, 왜 그것이 적절한 선택이라고 생각합니까? 아이템 컬렉션과 해당 단일 속성을 참조하는 컨테이너 뷰 모델 객체에 속성을 추가하는 것이 더 합리적이라고 생각됩니다. –

답변

0

최소한의 예입니다.

XAML : 뒤에

<Grid> 
    <Grid.Resources> 
     <ResourceDictionary> 
      <Style TargetType="{x:Type TreeViewItem}"> 
       <Setter Property="AllowDrop" Value="{Binding AllowDrop}"/> 
      </Style> 
     </ResourceDictionary> 
    </Grid.Resources> 

    <TreeView ItemsSource="{Binding MyItems}"> 
     <TreeView.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="{Binding Id}"/> 
        <TextBlock Margin="5,0" Text="{Binding AllowDrop}"/> 
       </StackPanel> 
      </DataTemplate> 
     </TreeView.ItemTemplate> 
    </TreeView> 
</Grid> 

코드 :

public partial class MainWindow : Window 
{ 
    public MainWindow() 
    { 
     InitializeComponent(); 

     DataContext = new ViewModel(); 
    } 
} 

public class ViewModel 
{ 
    public ViewModel() 
    { 
     MyItems = new List<ViewItem>(); 
     for (int i = 0; i < 10; i++) 
      MyItems.Add(new ViewItem { Id = i, AllowDrop = i % 2 == 0 }); 
    } 

    public List<ViewItem> MyItems { get; set; } 
} 

public class ViewItem 
{ 
    public int Id { get; set; } 
    public bool AllowDrop { get; set; } 
} 

는도 및 ID 항목에 작동합니다 TreeListView에 뭔가를 끌어 시도.