本当に奇妙な問題に遭遇しました。FooItemsControlViewModel
View( FooItemsControl
)に配置されたプロパティをバインドする方法がわかりません。
モデル:
public interface IFolder
{
string FullPath { get; }
string FolderLabel { get; }
List<IFolder> Folders { get; }
}
ビューモデル:
private ObservableCollection<IFolder> m_folders;
public ObservableCollection<IFolder> Folders
{
get { return m_folders; }
set
{
m_folders = value;
OnPropertyChanged("Folders");
}
}
XAML。このc は次の目的でDataTemplate
使用されます。<DataGridTextColumn.HeaderTemplate>
DataGrid
<DataGrid ItemsSource="{Binding Persons}">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding FirstName}" >
<DataGridTextColumn.HeaderTemplate>
...
<DataTemplate>
...
<PopUp>
<TreeView ItemsSource="{Binding DataContext.Folders, RelativeSource={RelativeSource AncestorType=UserControl, Mode=FindAncestor}, diag:PresentationTraceSources.TraceLevel=High}" Margin="10">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding DataContext.FooFolders, RelativeSource={RelativeSource AncestorType=UserControl, Mode=FindAncestor}}" DataType="{x:Type toDelete:IFolder}">
<TextBlock Text="{Binding FolderLabel}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
...
</PopUp>
</DataTemplate>
...
バインディングをデバッグするために、出力ウィンドウにトレースを追加しました。
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
出力ウィンドウに表示されるのは次のとおりです。
System.Windows.Data 警告: 78: BindingExpression (ハッシュ = 52875503): ルート項目 FooItemsControl (ハッシュ = 67071923) でアクティブ化します
System.Windows.Data 警告: 107: BindingExpression (ハッシュ = 52875503): FooItemsControl.DataContext のキャッシュされたアクセサーを使用するレベル 0: DependencyProperty(DataContext)
System.Windows.Data 警告: 104: BindingExpression (hash=52875503): アクセサー DependencyProperty(DataContext) を使用して、レベル 0 の項目を FooItemsControl (hash=67071923) に置き換えます
System.Windows.Data 警告: 101: BindingExpression (ハッシュ = 52875503): DependencyProperty(DataContext) を使用した FooItemsControl (ハッシュ = 67071923) からのレベル 0 の GetValue: FooItemsControlViewModel (ハッシュ = 28094463)
System.Windows.Data 警告: 107: BindingExpression (ハッシュ = 52875503): FooItemsControlViewModel.Folders のキャッシュされたアクセサーを使用するレベル 1: RuntimePropertyInfo(Folders)
System.Windows.Data 警告: 104: BindingExpression (ハッシュ = 52875503): アクセサー RuntimePropertyInfo(Folders) を使用して、レベル 1 のアイテムを FooItemsControlViewModel (ハッシュ = 28094463) に置き換えます
System.Windows.Data 警告: 101: BindingExpression (ハッシュ = 52875503): FooItemsControlViewModel (ハッシュ = 28094463) からのレベル 1 の GetValue は、RuntimePropertyInfo(Folders) を使用して:
System.Windows.Data 警告: 80: BindingExpression (ハッシュ = 52875503): TransferValue - 生の値を取得しました
System.Windows.Data 警告: 89: BindingExpression (ハッシュ = 52875503): TransferValue - 最終値を使用
また、Folders
プロパティは に示されていませんTreeView
。値が表示されない理由を知っている人はいますかTreeView
(以前に値を表示しました)。