简体   繁体   English

C#WPF在DataGrid中添加控件

[英]C# WPF add control in DataGrid

I want to add some controls in a DataGrid . 我想在DataGrid添加一些控件。

I have the XML code: 我有XML代码:

<DataGrid Name="dgCreateOperationsData" HorizontalScrollBarVisibility="Hidden"  HorizontalAlignment="Left" Height="214" Margin="2,170,0,0" VerticalAlignment="Top" Width="775" FontWeight="Bold" HeadersVisibility="None" SelectionMode="Single" Background="White"  CanUserAddRows="False" CanUserDeleteRows="False" ColumnWidth="100"/>

and the following code: 和以下代码:

OperationEntry opEntry = new OperationEntry();
opEntry.OperationName = new ComboBox() { Width = 50, ItemsSource = _operationList};
opEntry.Time = new TextBox() { Width = 50, Text = "" };
opEntry.Flow = new TextBox() { Width = 50, Text = "" };
opEntry.SysSpeed = new TextBox() { Width = 50, Text = "" };
opEntry.Pressure = new TextBox() { Width = 50, Text = "" };
opEntry.Torque = new TextBox() { Width = 50, Text = "" };
opEntry.Power = new TextBox() { Width = 50, Text = "" };
opEntry.Current = new TextBox() { Width = 50, Text = "" };

_operationEntryList.Add(opEntry);
dgCreateOperationsData.ItemsSource = _operationEntryList;

The problem is the controls are showing but have the control type visible, till i double-click it ( see image ) 问题是控件正在显示,但是控件类型可见,直到我双击它为止(参见图片)

在此处输入图片说明

Why is this happening? 为什么会这样呢? And how to remove these labels ?? 以及如何删除这些标签?

Below is the sample code. 下面是示例代码。 You need to work on it to finalize the functionality. 您需要对其进行处理才能完成功能。 One record is added through code and to add more records, you just need to press enter in last row. 通过代码添加一条记录,要添加更多记录,只需在最后一行按Enter。 It will automatically add the record to collection and you can do whatever you wish to with the data. 它将自动将记录添加到集合中,您可以对数据进行任何操作。

View - 查看-

<DataGrid Grid.Row="1" AutoGenerateColumns="False" ItemsSource="{Binding CountryList}" BorderThickness="0" HorizontalAlignment="Left">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Country Name" Binding="{Binding CountryName}"/>
            <DataGridComboBoxColumn Header="City Name"/>
        </DataGrid.Columns>
    </DataGrid>

ViewModel - ViewModel-

    public ObservableCollection<CountryData> CountryList { get; set; }

        CountryList = new ObservableCollection<CountryData>();
        CountryList.Add(new CountryData { CountryName = "India" });

You could define DataGridTemplateColumns with CellTemplates : 您可以使用CellTemplates定义DataGridTemplateColumns

...
dgCreateOperationsData.ItemsSource = _operationEntryList;
dgCreateOperationsData.AutoGeneratingColumn += (s, e) =>
{
    FrameworkElementFactory fe = new FrameworkElementFactory(typeof(ContentControl));
    fe.SetBinding(ContentControl.ContentProperty, new Binding(e.PropertyName));
    e.Column = new DataGridTemplateColumn()
    {
        CellTemplate = new DataTemplate() { VisualTree = fe }
    };
};

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

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