我正在用 C# 创建一个 WPF 应用程序。在我的窗口中有一个数据网格。网格中有 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>
</UserControl>
这是控件的 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"
当您想在
DataGridTemplateColumn
中显示自定义控件时,需要使用 DataGrid
...
您需要将
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>
这个现有问题应该可以帮助您解决您遇到的任何约束性问题......
您可以使用 TemplateColum。对于该列类型,您可以在 XAML 中定义内容。因此,您可以将控件放置在列模板内:
<DataGrid.Columns>
<DataGridTemplateColumn Header="TemplateColumn">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<YourControl></YourControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
根据我的知识,有两种方法可以做到这一点。
这个问题的解决方案
添加对您的 xaml 的引用。如果 xaml 找不到您的控件,通常是因为您忘记添加程序集。如果您引用其他项目的控件,则组装非常重要。
xmlns:controls="clr-namespace:MyControls;assembly=MyControls"
然后你可以使用像
这样的控件<controls:ControlClass....>
有时,您需要重新构建解决方案才能使智能感知正常工作(顺便说一句,智能感知很愚蠢,所以不要对智能感知/_/期望太高)
使用内容控制添加用户控制。 如果您想在代码隐藏中动态添加用户控件,通常会使用此解决方案。
首先在 .xaml 中添加这一行
<DataTemplate>
<ContentControl x:Name="ContentControl1"></ContentControl>
</DataTemplate>
然后在代码后面添加
ContentControl1.Content = new YourControlHere();