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

sa_ddam213 の回答により、bindItemsSource に使用される CollectionViewSource とのデフォルトの同期まで問題を追跡することができました

コンボボックスには次の属性が必要でした:

IsSynchronizedWithCurrentItem="False"
于 2013-01-24T19:10:42.027 に答える
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; }
}

結果:

ここに画像の説明を入力

于 2013-01-18T03:19:07.530 に答える