如何使用wpf将多个控件添加到数据网格的DataGridTemplateColumn?

时间:2021-05-20 19:36:37

I have several instances where I would like to have several controls in a single column in a datagrid.

我有几个实例,我想在数据网格中的单个列中有几个控件。

For example, I have a dataset that contains images with matching description, image source, timestamp, geotag, etc. I would like to display this information with a thumbnail image in one column and the majority of data in either a textbox or a label. Other datasets I have require textbox / checkbox, or textbox / combobox.

例如,我有一个数据集,其中包含具有匹配描述,图像源,时间戳,地理标记等的图像。我想在一列中显示此信息的缩略图图像,在文本框或标签中显示大部分数据。我需要的其他数据集是textbox / checkbox或textbox / combobox。

When I attempt to add a second control I receive an error reporting that The property "VisualTree" is set more than once.

当我尝试添加第二个控件时,我收到一个错误,报告属性“VisualTree”被设置多次。

<DataGridTemplateColumn Header="Data" Width="100">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Label Name="Description" Content="{Binding Desc}"></Label>
            <Label Name="Camera" Content="{Binding Camera}"></Label>
        </DataTemplate>      
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

1 个解决方案

#1


26  

The DataTemplate should have only one element, I believe - so you should use a Panel to contain the elements, say something like this:

我相信DataTemplate应该只有一个元素 - 所以你应该使用一个Panel来包含这些元素,比如说:

<DataGridTemplateColumn Header="Data" Width="100">
    <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
             <StackPanel Orientation="Horizontal">
                 <Label Name="Description" Content="{Binding Desc}"></Label>
                 <Label Name="Camera" Content="{Binding Camera}"></Label>
             </StackPanel>
         </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn> 

You could of course use WrapPanel, Grid, or anything else you like - StackPanel just appears to be what you're going for.

你当然可以使用WrapPanel,Grid或任何你喜欢的东西 - StackPanel似乎就是你想要的。

#1


26  

The DataTemplate should have only one element, I believe - so you should use a Panel to contain the elements, say something like this:

我相信DataTemplate应该只有一个元素 - 所以你应该使用一个Panel来包含这些元素,比如说:

<DataGridTemplateColumn Header="Data" Width="100">
    <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
             <StackPanel Orientation="Horizontal">
                 <Label Name="Description" Content="{Binding Desc}"></Label>
                 <Label Name="Camera" Content="{Binding Camera}"></Label>
             </StackPanel>
         </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn> 

You could of course use WrapPanel, Grid, or anything else you like - StackPanel just appears to be what you're going for.

你当然可以使用WrapPanel,Grid或任何你喜欢的东西 - StackPanel似乎就是你想要的。