簡體   English   中英

可以從ItemsControl的數據模板綁定組合框Itemssource

[英]Can one bind a combobox Itemssource from a datatemplate of a ItemsControl

給定的ItemsControl ComboBox將具有不同的項目,該項目基於該項目的DataContext (而不是ItemsControlDataContext )。 能做到嗎 如何? 最好從代碼后面。

我有以下DataModel

class Tester
{
    public Tester(string name, string surname)
    {
        Name = name;
        Surname = surname;
    }

    public string Name { get; set; }
    public string Surname { get; set; }
    public override string ToString()
    {
        return Name + " " + Surname;
    }
}

class TheT
{
    public ObservableCollection<Tester> TesterObject;

    public TheT()
    {
        TesterObject = new ObservableCollection<Tester>();
    }

    public string myDisplayName { get { return "test"; } }

    public void Add(Collection<Tester> col)
    {
        TesterObject.Clear();
        foreach (Tester t in col) { TesterObject.Add(t); }
    }
}

Window代碼中,我有:

ObservableCollection<TheT> myDataV ;
Public MainWindow()
{
    InitializeComponent();

    ObservableCollection<Tester> Tester1 = new ObservableCollection<Tester>();
    Tester1.Add(new Tester("Sunny", "Jenkins"));
    Tester1.Add(new Tester("Pieter", "Pan"));

    ObservableCollection<Tester> Tester2 = new ObservableCollection<Tester>();
    Tester2.Add(new Tester("Jack", "Sanders"));
    Tester2.Add(new Tester("Bill", "Trump"));

    myDataV = new ObservableCollection<TheT>();
    myDataV.Add(new TheT(Tester1));
    myDataV.Add(new TheT(Tester2));

    IControl.ItemsSource = myDataV;
    IControl.ItemTemplate = TestingDT;
}

IControl是在XAML設置的ItemsControl

<ItemsControl x:Name="IControl" Margin="53,375,81,63">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <VirtualizingStackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

還有我嘗試過各種方式的DataTemplate 但仍然無法顯示以下內容:

// the DataTemplate
private DataTemplate TestingDT
{
    get
    {
        DataTemplate DFT = new DataTemplate();
        DFT.DataType = typeof(TheT);

        FrameworkElementFactory Item = new FrameworkElementFactory(typeof(ComboBox));

        Binding B = new Binding("TesterObject")
        {
            Source = this
        };

        Item.SetBinding(ComboBox.ItemsSourceProperty, B);
        //Item.SetValue(ComboBox.DisplayMemberPathProperty, "Name");

        DFT.VisualTree = Item;
        return DFT;
    }
}

進行一些小的更改,您應該會得到預期的結果

public class Tester {
  public Tester(string name, string surname) {
    Name = name;
    Surname = surname;
  }

  public string Name { get; set; }

  public string Surname { get; set; }

  public override string ToString() {
    return Name + " " + Surname;
  }
}

public class TheT : DependencyObject {
  public static readonly DependencyProperty TesterObjectProperty =
    DependencyProperty.Register("TesterObject", typeof(ObservableCollection<Tester>), typeof(TheT),
                                new FrameworkPropertyMetadata());

  public ObservableCollection<Tester> TesterObject {
    get { return (ObservableCollection<Tester>)GetValue(TesterObjectProperty); }
    set { SetValue(TesterObjectProperty, value); }
  }

  public TheT(Collection<Tester> col) {
    TesterObject = new ObservableCollection<Tester>();
    foreach (Tester t in col) { TesterObject.Add(t); }
  }

  public void Add(Collection<Tester> col) {
    TesterObject.Clear();
    foreach (Tester t in col) { TesterObject.Add(t); }
  }
}

public Window1()
{
  InitializeComponent();

  ObservableCollection<Tester> Tester1 = new ObservableCollection<Tester>();
  Tester1.Add(new Tester("Sunny", "Jenkins"));
  Tester1.Add(new Tester("Pieter", "Pan"));

  ObservableCollection<Tester> Tester2 = new ObservableCollection<Tester>();
  Tester2.Add(new Tester("Jack", "Sanders"));
  Tester2.Add(new Tester("Bill", "Trump"));

  var myDataV = new ObservableCollection<TheT>();
  myDataV.Add(new TheT(Tester1));
  myDataV.Add(new TheT(Tester2));

  IControl.ItemsSource = myDataV;
}

XAML

<Window x:Class="stackoverflow.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:stackoverflow"
        Title="stackoverflow"
        Height="300"
        Width="300">

  <Grid>

    <ItemsControl x:Name="IControl" Margin="10">
      <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
          <VirtualizingStackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
      </ItemsControl.ItemsPanel>
      <ItemsControl.ItemTemplate>
        <DataTemplate DataType="{x:Type local:TheT}">
          <ComboBox ItemsSource="{Binding TesterObject}"
                    MinWidth="80"
                    DisplayMemberPath="Name" />
        </DataTemplate>
      </ItemsControl.ItemTemplate>
    </ItemsControl>

  </Grid>

</Window>

希望能有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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