1

私はItemsourceを持っていて、そこからデータグリッドの値をバインドしています

    <DataGridTextColumn Header="Message" Binding="{Binding Path=message}"/>
    <DataGridTextColumn Header=" Logger" Binding="{Binding Path=logger}"/>
    <DataGridTextColumn Header="Level" Binding="{Binding Path=level}" />

ヘッダーテキストもキーの辞書にバインドする必要があります

    Dictionary<String, object>.KeyCollection keyslist = dict1.Keys;

この辞書では、ヘッダー テキストをバインドする必要があります。

Datagrid に 2 つの itemsource を持つことは可能ですか??

4

1 に答える 1

0

あなたの質問に対する簡単な答えは: はい、可能です。これは面倒なやり方であり、この方法はお勧めしません (コンテキストの詳細がわからないため、本当に必要な場合を除きます) が、ここにアイデアがあります:

  1. 最初の問題は、ヘッダー バインディングが多少壊れているように見えることです。Source={x:Static} を使用して別のデータ コンテキストを定義する以外は、通常の方法でバインドできませんでした。

  2. ヘッダー バインディングではコレクションにバインドできないため、スケーラー値を指定するか、パラメーターを取得して辞書で参照して実際の値を返すコンバーターを作成する必要があります。

以下のサンプル コードは、バインディングをテストする方法を示しいます (コンバーターなし)。

XAML

<Window x:Name="window" x:Class="WpfDataGridMisc.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="214" DataContext="{Binding Source={x:Static wpfDataGridMisc:PersonsViewModel.Instance}}"
        xmlns:wpfDataGridMisc="clr-namespace:WpfDataGridMisc">
    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Persons}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding FirstName}" Header="{Binding Source={x:Static wpfDataGridMisc:PersonsViewModel.Instance}, Path=Header1}" />
        </DataGrid.Columns>
    </DataGrid>
</Window>

PersonsViewModel

using System;
using System.Collections.ObjectModel;

namespace WpfDataGridMisc
{
    public class PersonsViewModel
    {
        private static readonly PersonsViewModel Current = new PersonsViewModel();

        public static PersonsViewModel Instance { get { return Current; } }

        private PersonsViewModel()
        {
            Persons = new ObservableCollection<Person>
                {
                    new Person {FirstName = "Thomas", LastName = "Newman", Date = DateTime.Now},
                    new Person {FirstName = "Dave", LastName = "Smith", Date = DateTime.Now},
                };
            Header1 = "Header 1";
        }

        public ObservableCollection<Person> Persons { get; private set; }

        public string Header1 { get; set; }
    }
}

Person クラスは、上記のコードの Person から推定できる標準の poco です。

クレジット:大量のコードを提供してくれたJohan Larssonに感謝します。彼はこの解決策に取り組んでいて、私はただ助けただけでしたが、x:Static が私の考えだったので、彼は私が答えを投稿する必要があると感じました。

于 2013-01-21T18:20:50.450 に答える