0

これはロギング プロジェクトです。タイトルはアイテムの名前を提供し、メッセージは各段階をログに記録します。そのため、タイトルごとに多くのメッセージがあります。

質問をする目的で、オブジェクトを単純化しています。

私の Log クラスには 2 つのプロパティがあります。

List<LogDetails>
string Title

そして、私の LogDetails クラスには 1 つのプロパティがあります。

string Message

メッセージを XAML にバインドできません。タイトルは必要に応じてバインドします。

私のxamlコード:

<Window x:Class="BackUps.Logging.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:myData="clr-namespace:BackUps.Logging"
        Title="MainWindow"
        Height="350"
        Width="525">
  <Grid>
    <TreeView ItemsSource="{Binding}">
      <TreeView.ItemTemplate>
        <HierarchicalDataTemplate DataType="{x:Type myData:Log}"
                                  ItemsSource="{Binding LogDetailsList}">
          <TextBlock Text="{Binding Title}" />
          <HierarchicalDataTemplate.ItemTemplate>
            <DataTemplate DataType="{x:Type myData:LogDetails}">
              <StackPanel>
                <TextBlock Text="{Binding Message}" />
              </StackPanel>
            </DataTemplate>
          </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
      </TreeView.ItemTemplate>
    </TreeView>
  </Grid>

そして私のコードビハインド

  public MainWindow(List<Log> logs)
    {
        InitializeComponent();
        this.DataContext = logs;
    }

私の結果は次のとおりです(欠落しているエントリを確認できる場所):

ここに画像の説明を入力

これは Auto のウィンドウで、バインドしようとしているオブジェクトが表示されています。

ここに画像の説明を入力

私は何を見逃したか、間違っていますか?

4

1 に答える 1

1

過去に同様の問題があり、xaml を次の形式に (完全に) 変更して修正しました。

<Grid>
  <TreeView ItemsSource="{Binding}">
    <TreeView.Resources>
      <HierarchicalDataTemplate DataType="{x:Type myData:Log}"
                                ItemsSource="{Binding LogDetailsList}">
        <TextBlock Text="{Binding Title}" />
      </HierarchicalDataTemplate>
      <HierarchicalDataTemplate DataType="{x:Type myData:LogDetails}>
                        <TextBlock Text="{Binding Message}" />
      </HierarchicalDataTemplate>
    </TreeView.Resources>
  </TreeView>
</Grid>
于 2013-01-22T12:27:51.217 に答える