1

我对 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]

目前,树视图中的每个演示者姓名都正确显示,并且展开的第一个父项显示具有正确选择日期的组合框。但是,在任何时候显示的组合框都是“同步的”——即更改组合框中的值(或扩展不同的树视图项)会更改所有可见的组合框的值,因此它们都显示相同的日期。

<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>
4

2 回答 2

1

我已经编译了您的示例,但无法像您描述的那样使组合框同步。

也许您将能够看到我所做的不同并且可能是解决方法,或者也许我错了,并且在您的问题中遗漏了一些东西?

这是我使用的代码:

xml:

<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; }
}

结果:

在此处输入图像描述

于 2013-01-18T03:19:07.530 回答
1

由于 sa_ddam213 的回答,我能够将问题追踪到与用于 bindItemsSource 的 CollectionViewSource 的默认同步

组合框需要属性:

IsSynchronizedWithCurrentItem="False"
于 2013-01-24T19:10:42.027 回答