簡體   English   中英

將數據綁定到ItemsControl內的ItemsSource

[英]DataBinding an ItemsSource inside an ItemsControl

在wpf方面,我仍然將自己視為初學者。 我想知道兩件事

  1. 我哪里做錯了
  2. 我如何進行故障排除以找到解決方案。

情況:[數據部分]我有:數據模型對象。 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM