【问题标题】:C# WPF: Place UserControl in DataGridRowC# WPF:将 UserControl 放在 DataGridRow 中
【发布时间】:2015-07-15 12:26:45
【问题描述】:

我正在用 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"

【问题讨论】:

    标签: c# wpf xaml datagrid user-controls


    【解决方案1】:

    当您想在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>
    

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

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

    【讨论】:

    • 现在包含命名空间,我阅读了链接,正如 Denis Thomas 所说,我无法在我的窗口中拖动控件,并且在我的命名空间中找不到控件。命名空间为空。
    • 我无法解释添加命名空间比这更好...msdn.microsoft.com/en-gb/library/bb514546%28v=vs.90%29.aspx
    【解决方案2】:

    您可以使用 TemplateColum。对于该列类型,您可以在 XAML 中定义内容。因此,您可以将控件放在列模板中:

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

    【讨论】:

    • 我试过了,但是由于某种原因,没有找到我的控件名称。
    • 控件是否在 DataGrid 之外工作? (可能命名空间没有正确指定……)
    【解决方案3】:

    据我所知,有两种方法可以做到。

    1. 来自question的解决方案

      添加对您的 xaml 的引用。如果 xaml 无法正常找到您的控件,因为您忘记添加程序集。如果您要引用其他项目的控件,则组装非常重要。

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

      然后你就可以像使用控件了

      <controls:ControlClass....>
      

      有时,您需要重新构建解决方案以使智能感知正常工作(顺便说一句,智能感知很愚蠢,所以不要对智能感知/_/期望过高)

    2. 使用Content Control 添加用户控制。 如果您想在后面的代码中动态添加用户控件,通常会使用此解决方案。

      首先在 .xaml 中添加这一行

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

      然后在后面的代码中,添加

       ContentControl1.Content = new YourControlHere();
      

    【讨论】:

    • 该解决方案已经重新构建了很多次,还是找不到。该行也包括在内。
    猜你喜欢
    • 1970-01-01
    • 2012-12-25
    • 1970-01-01
    • 2023-04-02
    • 2023-04-08
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多