2

私はWPFを初めて使用するので、これが明らかな場合は申し訳ありませんが、インターネット上でそれがどのように行われるかを示す適切な例を見つけることができないようです。

MyCollectionというDataItemのコレクションにバインドされているDataGridがあります。グリッド内の複数の列(および必要に応じてアプリケーションの他の場所)に使用できる汎用DataTemplateを作成したいと思います。

例えば

<DataGrid ItemsSource="{Binding MyCollection}" AutoGenerateColumns="False" SelectionUnit="Cell" EnableColumnVirtualization="True">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="File path" CellTemplate="{StaticResource FileSelectorEditorTemplate}" CellEditingTemplate="{StaticResource FileSelectorEditorTemplate}" />
            <DataGridTemplateColumn Header="File path2" CellTemplate="{StaticResource FileSelectorEditorTemplate}" CellEditingTemplate="{StaticResource FileSelectorEditorTemplate}" />
            <DataGridTemplateColumn Header="File path3" CellTemplate="{StaticResource FileSelectorEditorTemplate}" CellEditingTemplate="{StaticResource FileSelectorEditorTemplate}" />
...

私のDataTemplateは、現在、アプリケーションリソースで次のように定義されています。

<DataTemplate x:Key="FileSelectorEditorTemplate">
        <Grid>
            <TextBox Text="{Binding FilePath.PhysicalPath}" HorizontalAlignment="Stretch" Margin="0,0,35,0" />
            <Button Content="..." Height="25" Width="25" Margin="0,0,5,0" HorizontalAlignment="Right" Click="FileOpen_Click" />
        </Grid>
    </DataTemplate>

ここで問題となるのは、バインディングがDataTemplateで指定されているのに対し、ビューモデルのプロパティFilePath、FilePath2、FilePath3ごとに異なるバインディングを適用する必要があることです。DataGridTemplateColumnでBindingを指定できないようですが?

正しい方向へのポインタをいただければ幸いです。

ありがとう!

4

2 に答える 2

0

のバインディングは、DataGridTemplateColumnその で指定されますCellTemplate。3 つの列に異なるバインドが必要な場合は、DataTemplate列ごとに異なるバインドを作成する必要があります。いくつかの回避策があるかもしれませんが、それがきれいになるとは思えません。

編集: さまざまなテンプレートを使用しDataTemplateSelectorて、現在のオブジェクトに適したテンプレートを選択できます。

IValueConverter の使用 (簡単なスケッチですが、動作するはずです):

<DataTemplate x:Key="GenericTemplate" >
        <TextBlock FontSize="14" >
            <TextBlock.Text>
                <Binding Converter="{StaticResource NewValue}" Path="Me" />
            </TextBlock.Text>
        </TextBlock>
</DataTemplate>

public class NewValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        someContainer obj = value as someContainer;
        if (obj.type == MyType.First)
             return (string)(obj.val1);
        else
             return (string)(obj.val2);
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

public enum MyType
{
    First,
    Second
}

public class someContainer
{
    public someContainer Me { get; set; }
    public string val1 { get; set; }
    public string val2 { get; set; }
    public MyType type;

    public someContainer()
    {
        Me = this;
        val1 = "string1";
        val2 = "string2";
    }     
}

...
public ObservableCollection<someContainer> myList {get; set;}
...

<StackPanel Margin="0,10,0,0" Orientation="Vertical" Grid.Column="2">
        <ItemsControl ItemsSource="{Binding MyList}" ItemTemplate="{StaticResource GenericTemplate}" />
    </StackPanel>
于 2012-12-13T14:51:10.767 に答える
0

Jesper Gaarsdal のオプションを使用できない場合は、CellStyle を使用して、列宣言でバインディングを定義することもできます。

たとえば、この SO を参照してください: WPF DataGridTemplateColumn を再利用する方法 (バインディングを含む)

于 2013-09-23T10:33:21.103 に答える