在treeview中使用wpf数据绑定组合框

时间:2013-01-18 00:32:23

标签: wpf data-binding treeview

我对WPF很新,并且非常感谢:

Model:
   Collection<Presenter>,
   Presenter has a Collection<Presentation>,
   Presentation has a TeachingSession property (which contains a DateTime? property)

我正在尝试显示树视图:

presenter name
   [combobox of available Dates]

此时,树视图中的每个演示者名称都正确显示,并且展开的第一个父项显示具有正确选择日期的组合框。但是,在任何时候显示的组合框都是同步的&#39; - 即更改组合框中的值(或扩展不同的树视图项)会更改可以看到的所有组合框的值,因此它们都显示相同的日期。

<TreeView Name="PresenterTreeView" ItemsSource="{Binding}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:Presenter}"
                  ItemsSource="{Binding Path=Presentations}">
            <TextBlock Text="{Binding Path=FullName}" />
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type local:Presentation}">
            <ComboBox SelectedItem="{Binding Path=TeachingSession, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                      DisplayMemberPath="SessionDate"
                      ItemsSource="{Binding Source={StaticResource availableDatesViewSource}}" >
            </ComboBox>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

2 个答案:

答案 0 :(得分:1)

我已经完成了你的例子,我无法像你所描述的那样让组合框同步。

也许你将能够看到我所做的不同,这可能是解决方案,或者我可能是错的,并且在你的问题中遗漏了什么?

这是我使用的代码:

的Xaml:

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="340" Width="480" Name="UI" xmlns:local="clr-namespace:WpfApplication8">

    <TreeView Name="PresenterTreeView" DataContext="{Binding ElementName=UI}" ItemsSource="{Binding Path=Presenters}" >
        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:Presenter}" ItemsSource="{Binding Path=Presentations}">
                <TextBlock Text="{Binding FullName}" />
            </HierarchicalDataTemplate>
            <HierarchicalDataTemplate DataType="{x:Type local:Presentation}">
                <ComboBox SelectedItem="{Binding TeachingSession.SessionDate}" ItemsSource="{Binding ElementName=UI, Path=Dates}" />
            </HierarchicalDataTemplate>
        </TreeView.Resources>
    </TreeView>
</Window>

代码:

public partial class MainWindow : Window
{
    private ObservableCollection<Presenter> _myProperty = new ObservableCollection<Presenter>();
    private ObservableCollection<DateTime?> _myDates = new ObservableCollection<DateTime?>();

    public MainWindow()
    {
        InitializeComponent();
        DateTime time1 = DateTime.Now;
        DateTime time2 = DateTime.Now.AddDays(1);
        Dates.Add(time1);
        Dates.Add(time2);
        for (int i = 0; i < 20; i++)
        {
            Dates.Add(DateTime.Now.AddDays(i));
        }

        TeachingSession teach = new TeachingSession { SessionDate = time1 };
        Presentation pres = new Presentation { TeachingSession = teach };
        Presenter presenter = new Presenter { FullName = "Presenter1" };
        presenter.Presentations = new ObservableCollection<Presentation>();
        presenter.Presentations.Add(pres);

        TeachingSession teach1 = new TeachingSession { SessionDate = time2 };
        Presentation pres1 = new Presentation { TeachingSession = teach1 };
        Presenter presenter1 = new Presenter { FullName = "Presenter1" };
        presenter1.Presentations = new ObservableCollection<Presentation>();
        presenter1.Presentations.Add(pres1);

        Presenters.Add(presenter);
        Presenters.Add(presenter1);
    }

    public ObservableCollection<Presenter> Presenters
    {
        get { return _myProperty; }
        set { _myProperty = value; }
    }

    public ObservableCollection<DateTime?> Dates
    {
        get { return _myDates; }
        set { _myDates = value; }
    }
}

public class Presenter
{
    public string FullName { get; set; }
    public ObservableCollection<Presentation> Presentations { get; set; }
}

public class Presentation
{
    public TeachingSession TeachingSession { get; set; }
}

public class TeachingSession
{
    public DateTime? SessionDate { get; set; }
}

结果:

enter image description here

答案 1 :(得分:0)

由于sa_ddam213的答案,我能够使用用于bindItemsSource的CollectionViewSource将问题跟踪到默认同步

组合框需要属性:

IsSynchronizedWithCurrentItem="False"