Skip to main content
 首页 » 编程设计

C# WPF : Place UserControl in DataGridRow

2024年02月06日14傻小

我正在用 C# 创建一个 WPF 应用程序。在我的窗口中有一个 DataGrid。在网格中有 2 列。第一列仅包含字符串。在第二列中,我想显示我创建的用户控件。

UserControl(称为:ProductControl)由 3 个按钮和 3 个文本框组成。

这是控件的 XAML 代码:

<UserControl x:Class="CARDS.ProductControl" 
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
         mc:Ignorable="d" Height="95" Width="273"> 
<Grid Margin="-23,0,0,0"> 
    <Grid.RowDefinitions> 
        <RowDefinition Height="10*"/> 
        <RowDefinition Height="24*"/> 
        <RowDefinition Height="29*"/> 
        <RowDefinition Height="32*"/> 
    </Grid.RowDefinitions> 
    <Button x:Name="btnSP_P" Content="SP/P" HorizontalAlignment="Left" Margin="215,3,0,0" VerticalAlignment="Top" Width="75" Grid.Row="2"/> 
    <Button x:Name="btnNM_M" Content="NM/M" HorizontalAlignment="Left" Margin="215,0,0,0" VerticalAlignment="Top" Width="75" Grid.Row="1"/> 
    <Button x:Name="btnHP" Content="HP" HorizontalAlignment="Left" Margin="215,4,0,0" VerticalAlignment="Top" Width="75" Grid.Row="3"/> 
    <TextBox x:Name="txtNM_M" HorizontalAlignment="Left" Height="23" Margin="27,0,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="183" Grid.Row="1"/> 
    <TextBox x:Name="txtSP_P" HorizontalAlignment="Left" Height="23" Margin="27,4,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="183" Grid.Row="2"/> 
    <TextBox x:Name="txtHP" HorizontalAlignment="Left" Height="23" Margin="27,3,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="183" Grid.Row="3"/> 
 
</Grid> 

这是控件的 C# 代码:

public partial class ProductControl : UserControl 
    { 
        public String NM_M { get; protected set; } 
        public String SP_P { get; protected set; } 
        public String HP { get; protected set; } 
 
        public ProductControl() 
        { 
            InitializeComponent(); 
        } 
 
        public void Set(String nm_m, String sp_p, String hp) 
        { 
            this.NM_M = nm_m; 
            this.SP_P = sp_p; 
            this.HP = hp; 
 
            txtHP.Text = hp; 
            txtNM_M.Text = nm_m; 
            txtSP_P.Text = sp_p; 
        } 
    } 

我有 2 个类包含需要在数据网格中显示的数据:

class DataItems { 
    public List<DataItemCard> items = new List<DataItemCard>(); 
} 
class DataItemCard { 
    public String Reference { get; set; } 
    public ProductControl Products { get; set; } 
} 

DataItems 的实例用作 DataGrid 的 ItemsSource。 字符串显示正确,但在第二列中只代表类型:'CARDS.ProductControl'。

DataGrid 在 XAML 文件中声明为:

<DataGrid x:Name="gridDisplayCards" HorizontalAlignment="Left" Margin="10,37,0,0" VerticalAlignment="Top" RenderTransformOrigin="3.046,4.843" Height="273" Width="284"> 

我的问题:如何在单元格中显示我的控件?

感谢大家的帮助和外部链接。现在可以了,问题实际上是装配。 我使用了:xmlns:controls="clr-namespace:OUTPOST_BUY_IN_SINGLE_CARDS;assembly=OUTPOST_BUY_IN_SINGLE_CARDS" 但它只需要:

xmlns:controls="clr-namespace:OUTPOST_BUY_IN_SINGLE_CARDS" 

请您参考如下方法:

当您想在 DataGrid 中显示自定义控件时,您需要使用 DataGridTemplateColumn ...

https://msdn.microsoft.com/en-us/library/system.windows.controls.datagridtemplatecolumn%28v=vs.110%29.aspx

您需要添加一个 xmlns 指令,例如 xmlns:controls="clr-namespace:MyControls;assembly=MyControls" 到您的窗口,然后引用该控件像这样:

       <DataGridTemplateColumn> 
            <DataGridTemplateColumn.CellTemplate> 
                <DataTemplate> 
                    <MyControls:ProductControl /><!--You will need to add your binding expressions to the ProductControl element--> 
                </DataTemplate> 
            </DataGridTemplateColumn.CellTemplate> 
        </DataGridTemplateColumn> 

这个现有问题应该可以帮助您解决您遇到的任何绑定(bind)问题......

How to bind a user control using as a DataGridTemplateColumn failed in WPF