简体   繁体   English

C#WPF:将UserControl放在DataGridRow中

[英]C# WPF: Place UserControl in DataGridRow

I'm creating a WPF Application in C#. 我正在用C#创建一个WPF应用程序。 In my window there is a DataGrid. 在我的窗口中有一个DataGrid。 In the Grid are 2 columns. 在网格中有2列。 The first Column contains only strings. 第一列仅包含字符串。 In the second column I want to display a usercontrol I created. 在第二列中,我要显示我创建的用户控件。

The UserControl (called: ProductControl) consists of 3 buttons and 3 textboxes. UserControl(称为:ProductControl)由3个按钮和3个文本框组成。

This is the XAML code for the control: 这是控件的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>

This is the C# code for the control: 这是控件的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;
        }
    }

I have 2 classes that contains the data that need to be displayed in the datagrid: 我有2个类,其中包含需要在datagrid中显示的数据:

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

The instance of DataItems is used as the ItemsSource for the DataGrid. DataItems的实例用作DataGrid的ItemsSource。 The String is displayed correctly, but in the second column just stands the type: 'CARDS.ProductControl'. 字符串显示正确,但在第二列中仅显示类型:“ CARDS.ProductControl”。

The DataGrid is declared in the XAML file as: 在XAML文件中,DataGrid声明为:

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

My question: How can I display my control in the cell? 我的问题:如何在单元格中显示控件?

Thank you all for the help and external links. 谢谢大家的帮助和外部链接。 It works now, the problem was actually the assembly. 现在可以正常工作了,实际上是组装问题。 I used: xmlns:controls="clr-namespace:OUTPOST_BUY_IN_SINGLE_CARDS;assembly=OUTPOST_BUY_IN_SINGLE_CARDS" But it just needed to be: 我曾经使用过: xmlns:controls="clr-namespace:OUTPOST_BUY_IN_SINGLE_CARDS;assembly=OUTPOST_BUY_IN_SINGLE_CARDS"但它只需要是:

xmlns:controls="clr-namespace:OUTPOST_BUY_IN_SINGLE_CARDS"

You need to use a DataGridTemplateColumn when you want to display a custom control within a DataGrid ... 要在DataGridTemplateColumn中显示自定义控件时,需要使用DataGrid

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

You will need to add an xmlns directive like xmlns:controls="clr-namespace:MyControls;assembly=MyControls" to your Window, and then reference the control like this: 您将需要在窗口中添加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>

This existing question should help you iron out any binding problems you have ... 这个现存的问题应该可以帮助您解决任何有约束力的问题...

How to bind a user control using as a DataGridTemplateColumn failed in WPF 如何在WPF中绑定用作DataGridTemplateColumn的用户控件失败

You could use a TemplateColum. 您可以使用TemplateColum。 For that column type you could define the content in XAML. 对于该列类型,您可以在XAML中定义内容。 Therefore you could place your control inside the column template: 因此,您可以将控件放在列模板中:

<DataGrid.Columns>
   <DataGridTemplateColumn Header="TemplateColumn">
       <DataGridTemplateColumn.CellTemplate>
           <DataTemplate>
               <YourControl></YourControl>
            </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>
   </DataGridTemplateColumn>
</DataGrid.Columns>

Based on my knowledge there are two way to do it. 根据我的知识,有两种方法可以做到这一点。

  1. Solution from this question 这个问题的解决方案

    Add a reference to your xaml. 添加对您的xaml的引用。 If xaml cannot find your control its normally because you forget to add assembly. 如果xaml找不到您的控件,那通常是因为您忘记添加程序集。 Assembly is very important if you are refering a control from other project. 如果您要引用其他项目中的控件,则汇编非常重要。

      xmlns:controls="clr-namespace:MyControls;assembly=MyControls" 

    Then you can use the control like 然后您可以使用类似的控件

     <controls:ControlClass....> 

    Sometimes, you need to rebuilt the solution to make the intellisense work (btw intellisense is stupid so don't expect too much from intellisense /_/) 有时,您需要重新构建解决方案以使智能感知正常工作(顺便说一句,智能感知是愚蠢的,因此不要对智能感知/ _ /期望过高)

  2. Using Content Control to add user control. 使用内容控件添加用户控件。 This solution is normally used if you want to add the user control dynamically in code behind. 如果要在后面的代码中动态添加用户控件,通常使用此解决方案。

    First you add this line in .xaml 首先,您在.xaml中添加此行

     <DataTemplate> <ContentControl x:Name="ContentControl1"></ContentControl> </DataTemplate> 

    Then in code behind, add 然后在后面的代码中添加

      ContentControl1.Content = new YourControlHere(); 

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

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