我有一个列表框及其数据模板。但在其DataTemplate中我有CheckedCombobox 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:
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 :(得分:0)
这是因为您需要在内部绑定上使用RelativeSource来获取正确的DataContext,如下所示:
foobar
在这里有关于RelativeSource的许多其他文章,应该为您提供所需的文章。