2

I'm very new to WPF so sorry if this is obvious but I can't seem to find any decent examples on the internet showing how it's done.

I have a DataGrid which is bound to a collection of DataItem called MyCollection. I want to create a generic DataTemplate that I can use for multiple columns in the grid (and elsewhere in the application should I need it).

E.g.

<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}" />
...

My DataTemplate is defined at the moment in my Application resources as

<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>

Now the problem is that the binding is specified in the DataTemplate, whereas I need to apply a different binding for each of the properties FilePath, FilePath2, FilePath3 on the view model. I don't seem to be able to specify the Binding on the DataGridTemplateColumn though?

I'd appreciate any pointers in the right direction,

Thanks!

4

2 回答 2

0

如果您不能使用 Jesper Gaarsdal 的选项,您也可以使用 CellStyle 并在列声明中定义绑定。

例如,请参阅此 SO:如何重用 WPF DataGridTemplateColumn(包括绑定)

于 2013-09-23T10:33:21.103 回答
0

上的绑定DataGridTemplateColumn在其CellTemplate. 如果您想要三列的不同绑定,我会说您必须DataTemplate为每一列设置不同的绑定。可能有一些解决方法,但我怀疑它会很漂亮。

编辑:拥有不同的模板,您可以使用 aDataTemplateSelector为当前对象选择正确的模板。

使用 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 回答