Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF DataGridTemplateColumn,绑定到通用CellTemplate_Wpf_Datagrid_Datatemplate_Datagridtemplatecolumn - Fatal编程技术网

WPF DataGridTemplateColumn,绑定到通用CellTemplate

WPF DataGridTemplateColumn,绑定到通用CellTemplate,wpf,datagrid,datatemplate,datagridtemplatecolumn,Wpf,Datagrid,Datatemplate,Datagridtemplatecolumn,我是WPF的新手,很抱歉,如果这是显而易见的,但我似乎在互联网上找不到任何合适的例子来说明它是如何做到的 我有一个DataGrid,它绑定到一个名为MyCollection的DataItem集合。我想创建一个通用的DataTemplate,可以用于网格中的多个列以及应用程序中其他需要的列 例如 我的DataTemplate目前在我的应用程序资源中定义为 <DataTemplate x:Key="FileSelectorEditorTemplate"> <Grid

我是WPF的新手,很抱歉,如果这是显而易见的,但我似乎在互联网上找不到任何合适的例子来说明它是如何做到的

我有一个DataGrid,它绑定到一个名为MyCollection的DataItem集合。我想创建一个通用的DataTemplate,可以用于网格中的多个列以及应用程序中其他需要的列

例如

我的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上指定绑定

如果能给我指出正确的方向,我将不胜感激

谢谢

DataGridTemplateColumn上的绑定在其CellTemplate中指定。 如果您想为这三列创建不同的绑定,我认为您必须为每一列创建不同的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>

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


例如,看看这个:

我觉得这很可笑。当然,WPF中必须有一种方法来创建一个公共模板,我可以在整个应用程序中为相同类型的不同属性重用该模板?也许您可以在DataTemplate中的绑定上使用IValueConverter。我在这里发布了一个例子:谢谢Jesper,但我不确定这是否有帮助?我是否仍然存在如何在数据模板中指定绑定的问题?添加了一些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>