2017-12-05 10 views
-1

プロパティAllowDropを持つモデルクラスViewItemがあります。私のビューモデルViewModelは、観察可能なViewItemのコレクションです。WPFツリービューのViewModelでモデルのAllowDropプロパティを取得

ViewItemプロパティ

public bool AllowDrop 
    { 
     get 
     { 
      return _allowDrop; 
     } 
    } 

私はViewTree thatsのデータソースがViewModelに、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; } 
} 

がTreeListViewに何かをドラッグしようとするも、IDを持つアイテムのみで動作します。

関連する問題