Wpf 选中Listbox DataTemplate内的DataTemplate内的Combobox
我有一个列表框和它的数据模板。但在它的DataTemplate中,我选中了ComboBox DataTemplate。想知道如何设置绑定。我尝试了以下方法来获取每个listbox项在子元素中签入的项。下面是不起作用的代码Wpf 选中Listbox DataTemplate内的DataTemplate内的Combobox,wpf,mvvm,combobox,datatemplate,Wpf,Mvvm,Combobox,Datatemplate,我有一个列表框和它的数据模板。但在它的DataTemplate中,我选中了ComboBox DataTemplate。想知道如何设置绑定。我尝试了以下方法来获取每个listbox项在子元素中签入的项。下面是不起作用的代码 <ListBox Name="ListLayers" ItemsSource="{Binding LstDragList}" Height="123" Width="283" SelectedIndex="{Binding
<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); }
}
}
公共类DragList:observateObject
{
公共DragList()
{
_selectedLayers=string.Empty;
}
公共字符串文件名{get;set;}
公共可观测集合lstLayerModel{get;set;}
公共字符串文本{get;set;}
私有字符串\u选择的层;
公共字符串选择层
{
获取{return\u selectedLayers;}
set{SetAndNotify(ref _selectedLayers,value,()=>this.selectedLayers);}
}
专用int_selectedLayerInx;
公共整数选择层
{
获取{return\u 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;
}
公共视图模型()
{
_lstLayers=新的ObservableCollection();
mCheckedItems=新的ObservableCollection();
_tempDragList=新列表();
_lstLayers.CollectionChanged+=\lstLayers\u CollectionChanged;
_lstLayers.Add(新模型
{
LayerName=“全部”,
LayerNumber=“”,
IsChecked=true
});
_添加(新模型{LayerName=“layerOne”,LayerNumber=“1”,IsChecked=false});
_添加(新模型{LayerName=“第二层”,LayerNumber=“2”,IsChecked=false});
_添加(新模型{LayerName=“第三层”,LayerNumber=“3”,IsChecked=false});
_lstDragList=新列表();
_添加(新的DragList{FileName=“Test”,lstLayerModel=lstLayers});
_添加(新的DragList{FileName=“Test1”,lstLayerModel=lstLayers});
_tempDragList=lstDragList;
}
这是因为您需要在内部绑定上使用,以获得正确的DataContext,如下所示:
{Binding DataContext.SelectedRow, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}
这里还有许多其他关于RelativeSource的文章,可以为您提供所需的信息。您能准确地解释一下什么不起作用吗?LstDragList实际上是viewmodel中公共属性的名称吗?Model是否具有您试图绑定到的属性?在运行时使用Snoop检查绑定(以及相关的DataContext)。我有一个列表,在这个模型中我有上面提到的列表。combobox的IsChecked属性在运行时与列表绑定,我们有更多的列表项,但当我选中选中选中项的第一个combobox时,它也会反映combobox选中属性的第二个项。通常,datatemplate绑定应该与List绑定。