![](/img/trans.png)
[英]Attaching ObservableCollection as ItemsSource of ItemsControl
[英]DataBinding an ItemsSource inside an ItemsControl
在wpf方面,我仍然将自己视为初学者。 我想知道两件事
情况:[数据部分]我有:数据模型对象。 DataFilter对象,它基本上是DataModels +添加的函数的集合。 还有DataFiltersGroup,它用于DataViewModel中并具有DataFilters的集合,我有一个DataViewModel对象,该对象基本上是可观察的项目集合。 我想在Itemscontrol中显示每个DataFilter。
[当前解决方案]我已经构建了一个特殊的Combo控件,该控件从combobox(基本上是一个按钮+ combobox)派生而来。 特意绑定时,combocombo可以正常工作。 因此,我非常有信心这个问题不在于特殊组合。 当我将ItemsControl.ItemsSource属性设置为DataFilters的集合并制作SpecialCombo的DataTemplate时,组合框不显示任何结果(如果没有Items,则特殊组合将不显示切换按钮-仅显示按钮)。 一种替代方法-下面的绑定方法(2)让我看到下拉切换按钮,但是下拉菜单为空,但是我知道不应该这样。
这是摘要的代码摘录
public class DataModel :INotifyPropertyChanged
{
//The Item!!
public int Index {//Normal get set property}
public string Name {//Normal get set property}
public int Parent {//Normal get set property}
public string FullName {//Normal get set property}
public string DisplayName {//Normal get set property}
public bool Static {//Normal get set property}
}
public class DataFilters : DataCollection
{
public ObservableCollection<DataModel> CombinedData;
public int FilterIndex{//Property... The index of the current item like Index for DataModel.}
public string ParentName {//property ButtonContent item}
public int SelectedItem {//Property}
}
//Used as part of DataVieModel. Also responsible of building each DataFilters item and some other functions
public class DataFilterGroup : INotifyPropertyChanged
{
public ObservableCollection<DataFilters> FullCollection;
}
WPF对象
<ItemsControl x:Name="PART_ListBox" HorizontalAlignment="Left" VerticalContentAlignment="Stretch" Margin="0">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
WPF加载后的代码
//DVM = DataVieModel with some other objects. Filters = DataFilterGroup
PART_ListBox.ItemsSource = DVM.Filters.FullCollection;
PART_ListBox.ItemTemplate = DataFilterTemplate;
//And DataTemplate (1) - shows no combobox
private static DataTemplate DataFilterTemplate
{
get
{
DataTemplate DFT = new DataTemplate();
DFT.DataType = typeof(DataFilters);
FrameworkElementFactory Stack = new FrameworkElementFactory(typeof(VirtualizingStackPanel));
Stack.SetValue(VirtualizingStackPanel.OrientationProperty, Orientation.Horizontal);
FrameworkElementFactory Item = new FrameworkElementFactory(typeof(SpecialCombo));
Item.SetValue(SpecialCombo.ButtonContentProperty, new Binding("ParentName"));
Item.SetValue(SpecialCombo.ItemsSourceProperty, "CombinedData");
Item.SetValue(SpecialCombo.DisplayMemberPathProperty, "DisplayName");
Item.SetValue(SpecialCombo.SelectedValuePathProperty, "Index");
Item.SetValue(SpecialCombo.SelectedValueProperty, "SelectedItem");
//Item.SetValue(SpecialCombo.ToggleVisibleProperty, new Binding("ComboVisibility"));
//Item.SetValue(SpecialCombo.SelectedValueProperty, new Binding("SelectedItem"));
Stack.AppendChild(Item);
DFT.VisualTree = Stack;
return DFT;
}
}
//And DataTemplate (2) - shows combobox with no items in dropdown
private static DataTemplate DataFilterTemplate
{
get
{
DataTemplate DFT = new DataTemplate();
DFT.DataType = typeof(DataFilters);
FrameworkElementFactory Stack = new FrameworkElementFactory(typeof(VirtualizingStackPanel));
Stack.SetValue(VirtualizingStackPanel.OrientationProperty, Orientation.Horizontal);
FrameworkElementFactory Item = new FrameworkElementFactory(typeof(SpecialCombo));
Item.SetValue(SpecialCombo.ButtonContentProperty, new Binding("ParentName"));
Item.SetValue(SpecialCombo.ItemsSourceProperty, new Binding("CombinedData"));
Item.SetValue(SpecialCombo.DisplayMemberPathProperty, "DisplayName");
Item.SetValue(SpecialCombo.SelectedValuePathProperty, "Index");
Item.SetValue(SpecialCombo.SelectedValueProperty, "SelectedItem");
//Item.SetValue(SpecialCombo.ToggleVisibleProperty, new Binding("ComboVisibility"));
//Item.SetValue(SpecialCombo.SelectedValueProperty, new Binding("SelectedItem"));
Stack.AppendChild(Item);
DFT.VisualTree = Stack;
return DFT;
}
}
多亏了punker 76在另一篇文章中我对问题进行了结构调整(这里-> 可以从ItemsControl的数据模板绑定combobox Itemssource)吗 ?
因此,DataFilters应该成为一个依赖对象
public class DataFilters : DataCollection
// should become
public class DataFilters : DependencyObject
Observalbe收集也应更改。 所以
public ObservableCollection<DataModel> CombinedData;
// should become
public static readonly DependencyProperty CombinedData= DependencyProperty.Register("CombinedData", typeof(ObservableCollection<DataModel>), typeof(DataFilters), new FrameworkPropertyMetadata());
//and should become a property
public ObservableCollection<DataModel> CombinedData
{
get { return (ObservableCollection<DataModel>)GetValue(CombinedDataProperty); }
set { SetValue(CombinedDataProperty, value); }
}
然后在DataTemplate文件中,以下内容应更改Item.SetValue(SpecialCombo.ItemsSourceProperty,“ CombinedData”); //应更改为Item.SetBinding(SpecialCombo.ItemsSourceProperty,new Binding(“ CombinedData”));
我还没有遍历上面的整个代码,但是我认为,为了在DataTemplate中绑定组合框类型的项目,应该全部进行上述所有更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.