在wpf方面,我仍然认为自己是初学者。我想知道两件事
情况:[数据部分]我有: DataModel对象。 DataFilter对象基本上是DataModels +添加函数的集合。和DataFiltersGroup,在DataViewModel中使用并具有DataFilters的集合我有一个DataViewModel对象,它基本上是一个可观察的项集合。我想在Itemscontrol中显示每个DataFilter。
[当前解决方案]我已经构建了一个特殊的combo控件,它源自组合框[基本上是一个按钮+组合框]。故意绑定时,specialcombo工作正常。所以我相信这个问题不是特别的组合。当我将ItemsControl.ItemsSource属性设置为DataFilters的集合并生成SpecialCombo的DataTemplate时,组合框不显示任何结果(如果没有项目,则特殊组合将不显示切换按钮 - 仅显示按钮)。另一种方法 - 下面的绑定方法(2)让我看到下拉togglebutton,但下拉列表是空的,但我知道它不应该是。
这是代码的sumarized提取
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;
}
}
答案 0 :(得分:0)
感谢punker 76在另一篇文章中重新构建了问题(这里 - &gt; Can one bind a combobox Itemssource from a datatemplate of a ItemsControl),我必须完成以下工作。
DataFilters应该成为一个dependencyobject因此
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中绑定组合框类型的项目,所有上述更改都应该是必需的。