2017-05-25 75 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); } 
    } 

} 

我的構造視圖模型:

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實際上是viewmodel中公共屬性的名稱嗎? –

+0

模型是否有你想要綁定的屬性? –

+0

使用Snoop在運行時檢查綁定(以及相關的DataContext)。 – Will

回答

0

這是因爲你需要使用在內部綁定RelativeSource,以獲得正確的DataContext像這樣:

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

SO上有很多關於RelativeSource的文章,應該爲您提供所需的內容。

相關問題