简体   繁体   English

在 Silverlight 数据网格中添加一行控件

[英]Adding a row of controls in a Silverlight Datagrid

In the application I'm developing, I'm using a datagrid to display data fetch from a database.在我正在开发的应用程序中,我使用数据网格来显示从数据库中获取的数据。 It is declared like so in my XAML file:在我的 XAML 文件中声明如下:

<sdk:DataGrid x:Name="dg" AutoGenerateColumns="False" ItemSource={Binding etc.} >
    <sdk:DataGrid.Columns>
        <sdk:DataGridTextBoxColumn Header="Col1"
             Binding="{Binding Col1Data}" />
        <sdk:DataGridTextBoxColumn Header="Col2" 
             Binding="{Binding Col2Data}" />
        <sdk:DataGridTextBoxColumn Header="Col3"
             Binding="{Binding Col3Data}" />
        <sdk:DataGridCheckBoxColumn Header="Col4" 
             Binding="{Binding Col4Data}" />
   <sdk:DataGrid.Columns>
<sdk:DataGrid>

What I want to do, is to add a row, containing 5 combo boxes (1 for each column) between the header and the first row of my data.我想要做的是在 header 和我的数据的第一行之间添加一行,其中包含 5 个组合框(每列 1 个)。 It is pretty easy to do for a column, using DataGridTemplateColumn, but how can I do this for a row?使用 DataGridTemplateColumn 对列执行此操作非常容易,但是如何对行执行此操作?

Thanks in advance for your suggestions!提前感谢您的建议!

Fake edit: Oh by the way I'm not a fan of code behind (trying to go full MVVM), so I'm looking for a way to do this in XAML, if possible.假编辑:哦,顺便说一句,我不喜欢背后的代码(尝试 go 完整的 MVVM),所以如果可能的话,我正在寻找一种在 XAML 中执行此操作的方法。

You may be able to get away with providing a template for the header, but failing that you'll need to re-template the DataGrid in order to do this.您可能能够为 header 提供模板,但如果不这样做,您需要重新模板化DataGrid才能执行此操作。

Ok, so I kinda found a work around.好的,所以我找到了一个解决方法。 I declared a template for my cells that contains a button and a textblock bound to the data.我为我的单元格声明了一个模板,其中包含一个按钮和一个绑定到数据的文本块。 i bind the visibility property of the button on a boolean that will be true only for the elements of the first row.我在 boolean 上绑定按钮的可见性属性,该属性仅适用于第一行的元素。

<sdk:DataGridTemplateColumn Header="Col1" Width="60">
    <sdk:DataGridTemplateColumn.CellTemplate>
       <DataTemplate>
          <StackPanel Orientation="Vertical" Height="30">
             <Button Content="Boutton" Visibility="{Binding Path=IsFirstElement, Converter={StaticResource visibilityConverter}}" />
             <TextBlock Text="{Binding Path=Col1Data}" />
           </StackPanel>
        </DataTemplate>
     </sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>

It's a bit hack-ish.这有点hack-ish。 But it works.但它有效。 My only concern is performance since a button is declared for each cell.我唯一关心的是性能,因为每个单元格都声明了一个按钮。 So with thousands of row, I guess there could be a performance hit.因此,有数千行,我想可能会对性能造成影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM