C# WPF:将 UserControl 放在 DataGridRow 中
C# WPF: Place UserControl in DataGridRow
我正在用 C# 创建一个 WPF 应用程序。在我的 window 中有一个 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"
您可以使用 TemplateColum。对于该列类型,您可以在 XAML 中定义内容。因此,您可以将控件放在列模板中:
<DataGrid.Columns>
<DataGridTemplateColumn Header="TemplateColumn">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<YourControl></YourControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
如果要在 DataGrid
中显示自定义控件,则需要使用 DataGridTemplateColumn
...
您需要向 Window 添加一个 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
据我所知,有两种方法。
来自这个question
的解决方案
添加对您的 xaml 的引用。如果 xaml 无法正常找到您的控件,因为您忘记添加程序集。如果您要引用其他项目的控件,则汇编非常重要。
xmlns:controls="clr-namespace:MyControls;assembly=MyControls"
然后就可以像
那样使用控件了
<controls:ControlClass....>
有时,您需要重建解决方案以使智能感知正常工作(顺便说一句,智能感知很愚蠢,所以不要对智能感知期望过高/_/)
使用Content Control添加用户控件。
如果您想在代码隐藏中动态添加用户控件,通常会使用此解决方案。
首先在 .xaml
中添加这一行
<DataTemplate>
<ContentControl x:Name="ContentControl1"></ContentControl>
</DataTemplate>
然后在后面的代码中,添加
ContentControl1.Content = new YourControlHere();
我正在用 C# 创建一个 WPF 应用程序。在我的 window 中有一个 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"
您可以使用 TemplateColum。对于该列类型,您可以在 XAML 中定义内容。因此,您可以将控件放在列模板中:
<DataGrid.Columns>
<DataGridTemplateColumn Header="TemplateColumn">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<YourControl></YourControl>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
如果要在 DataGrid
中显示自定义控件,则需要使用 DataGridTemplateColumn
...
您需要向 Window 添加一个 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
据我所知,有两种方法。
来自这个question
的解决方案添加对您的 xaml 的引用。如果 xaml 无法正常找到您的控件,因为您忘记添加程序集。如果您要引用其他项目的控件,则汇编非常重要。
xmlns:controls="clr-namespace:MyControls;assembly=MyControls"
然后就可以像
那样使用控件了<controls:ControlClass....>
有时,您需要重建解决方案以使智能感知正常工作(顺便说一句,智能感知很愚蠢,所以不要对智能感知期望过高/_/)
使用Content Control添加用户控件。 如果您想在代码隐藏中动态添加用户控件,通常会使用此解决方案。
首先在 .xaml
中添加这一行<DataTemplate> <ContentControl x:Name="ContentControl1"></ContentControl> </DataTemplate>
然后在后面的代码中,添加
ContentControl1.Content = new YourControlHere();