2017-05-25 14 views
0

私はリストボックスとそのデータテンプレートを持っています。しかし、DataTemplateには私はCheckedCombobox DataTemplateを持っています。バインディングの設定方法を知りたい私は各項目の子要素でチェックされている項目を取得するために以下のことを試しました。以下は動作していないコードです。リストボックス内にあるDataTemplate内のチェックコンボボックスDataTemplate

<ListBox 
    Name="ListLayers" 
    ItemsSource="{Binding LstDragList}" 
    Height="123" 
    Width="283" 
    SelectedIndex="{Binding SelectedRow, UpdateSourceTrigger=PropertyChanged, Mode=OneWayToSource}" 
    > 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <ComboBox 
        Name="childlayers" 
        Style="{StaticResource EditableCboStyle}" 
        ItemsSource="{Binding lstLayerModel}" 
        Text="{Binding SelectedLayers, UpdateSourceTrigger=PropertyChanged}" 
        ItemContainerStyle="{DynamicResource ComboboxItemContainerStyle}" 
        Width="200" 
        IsEditable="True" 
        IsReadOnly="False" 
        PreviewMouseLeftButtonDown="ComboBox_PreviewMouseLeftButtonDown" 
        > 
        <ComboBox.ItemTemplate> 
         <DataTemplate DataType="{x:Type local:Model}"> 
          <CheckBox 
           VerticalAlignment="Center" 
           VerticalContentAlignment="Center" 
           IsChecked="{Binding IsChecked}" 
           Content="{Binding DisplayLayer}" 
           /> 
         </DataTemplate> 
        </ComboBox.ItemTemplate> 
       </ComboBox> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

モデル:

public class DragList : ObservableObject 
{ 
    public DragList() 
    { 
     _selectedLayers = string.Empty; 
    } 
    public string FileName { get; set; } 
    public ObservableCollection<Model> lstLayerModel { get; set; } 
    public string Text { get; set; } 

    private string _selectedLayers; 

    public string SelectedLayers 
    { 
     get { return _selectedLayers; } 
     set { SetAndNotify(ref _selectedLayers, value,() => this.SelectedLayers); } 
    } 

    private int _selectedLayerInx; 

    public int SelectedLayerInx 
    { 
     get { return _selectedLayerInx; } 
     set { SetAndNotify(ref _selectedLayerInx, value,() => this.SelectedLayerInx); } 
    } 

} 

私のコンストラクタのviewmodel:あなたがそうのように正しいのDataContextを取得するために、内部のバインディングにRelativeSourceを使用する必要があるためです

public ViewModel() 
{ 
    _lstLayers = new ObservableCollection<Model>(); 
    mCheckedItems = new ObservableCollection<Model>(); 
    _tempDragList = new List<DragList>(); 

    _lstLayers.CollectionChanged += _lstLayers_CollectionChanged; 
    _lstLayers.Add(new Model 
    { 
     LayerName = "All", 
     LayerNumber = "", 

     IsChecked = true 
    }); 
    _lstLayers.Add(new Model { LayerName = "Layer one", LayerNumber = "1", IsChecked = false }); 
    _lstLayers.Add(new Model { LayerName = "Layer two", LayerNumber = "2", IsChecked = false }); 
    _lstLayers.Add(new Model { LayerName = "Layer three", LayerNumber = "3", IsChecked = false }); 

    _lstDragList = new List<DragList>(); 
    _lstDragList.Add(new DragList { FileName = "Test", lstLayerModel = _lstLayers }); 
    _lstDragList.Add(new DragList { FileName = "Test1", lstLayerModel = _lstLayers }); 

    _tempDragList = _lstDragList; 
} 
+0

正常に機能していないものを正確に説明できますか? LstDragListは実際にビューモデル内のパブリックプロパティの名前ですか? –

+0

モデルにはバインドしようとしているプロパティがありますか? –

+0

Snoopを使用して、実行時にバインディング(および関連するDataContext)を調べます。 – Will

答えて

0

{Binding DataContext.SelectedRow, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}} 

あなたに必要なものを提供する必要があるので、ここにRelativeSourceに関する他の多くの記事があります。

関連する問題