簡體   English   中英

C#WPF:將UserControl放在DataGridRow中

[英]C# WPF: Place UserControl in DataGridRow

我正在用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個類,其中包含需要在datagrid中顯示的數據:

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”。

在XAML文件中,DataGrid聲明為:

<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

https://msdn.microsoft.com/zh-cn/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>

這個現存的問題應該可以幫助您解決任何有約束力的問題...

如何在WPF中綁定用作DataGridTemplateColumn的用戶控件失敗

您可以使用TemplateColum。 對於該列類型,您可以在XAML中定義內容。 因此,您可以將控件放在列模板中:

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

根據我的知識,有兩種方法可以做到這一點。

  1. 這個問題的解決方案

    添加對您的xaml的引用。 如果xaml找不到您的控件,那通常是因為您忘記添加程序集。 如果您要引用其他項目中的控件,則匯編非常重要。

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

    然后您可以使用類似的控件

     <controls:ControlClass....> 

    有時,您需要重新構建解決方案以使智能感知正常工作(順便說一句,智能感知是愚蠢的,因此不要對智能感知/ _ /期望過高)

  2. 使用內容控件添加用戶控件。 如果要在后面的代碼中動態添加用戶控件,通常使用此解決方案。

    首先,您在.xaml中添加此行

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

    然后在后面的代碼中添加

      ContentControl1.Content = new YourControlHere(); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM