首页 > 解决方案 > How to add a textbox row inside the datagrid wpf

问题描述

I have a Datagrid that binds and auto generate columns. I need to add a row inside the table for searchbox in each column of DataGridTextColumn type alone.

I need to create like this in the photo belowenter image description here

I have searched a lot but i can't find any solution for it. Anyone know how to create the textbox inside datagrid. Some links or sample codes will be helpful.

标签: c#wpf

解决方案


您可以修改ItemsPanel为其他控件创建间隙:

    <DataGrid.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel IsItemsHost="True" Margin="0,50,0,0"/>
        </ItemsPanelTemplate>
    </DataGrid.ItemsPanel>

然后将其他文本框覆盖到DataGrid使用 aCanvas或任何其他ClipToBounds等于 false的控件的区域上

在此处输入图像描述

<Grid>
    <DataGrid ItemsSource="{Binding ...}" >
        <DataGrid.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel IsItemsHost="True" Margin="0,50,0,0"/>
            </ItemsPanelTemplate>
        </DataGrid.ItemsPanel>
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding ...}" x:Name="col1"/>
            <DataGridTextColumn Binding="{Binding ...}" x:Name="col2"/>
        </DataGrid.Columns>
    </DataGrid>
    <Canvas>
        <TextBox Height="30" Width="{Binding ElementName=col1, Path=ActualWidth}" Margin="10,30,0,0"/>
        <TextBox Height="30" Width="{Binding ElementName=col2, Path=ActualWidth}" Margin="50,30,0,0"/>
    </Canvas>
</Grid>

我将跳过使用转换器将每个文本框的边距绑定到列宽的部分,因为编写代码对我来说完全是微不足道的。

此处的其他解决方法


推荐阅读