简体   繁体   English

将仅一个DataGrid行设置为可通过编程方式进行编辑

[英]Set only one DataGrid Row to be editable programmatically

I am filling up my DataGrid with information from a database. 我正在用来自数据库的信息填充DataGrid。 The information is appearing fine. 该信息似乎很好。 I want to keep everything as read only less one column. 我希望将所有内容保留为少于一列。 I do not want that column header to be editable. 我不希望该列标题可编辑。 Only the rows that come under it. 只有它下面的行。 for example as per the image below, I want to be able to edit the column Participants row content 5,5,5 and 3. 例如,根据下面的图片,我希望能够编辑“参加者”行的内容5,5,5和3。

I don't see any options to be able individually select a row or column to set as read only. 我看不到任何能够单独选择要设置为只读的行或列的选项。 I only see an option to set the entire DataGrid as read only. 我只看到一个将整个DataGrid设置为只读的选项。 I was trying to loop through and attempt to set read only to the desired column but couldn't. 我试图遍历,并尝试将只读设置为所需的列,但是不能。 Please advice. 请指教。 Thanks. 谢谢。

在此处输入图片说明

Saw some examples where you can individually state which column to be readonly via XAML but those were manual inputs for pre-determined data. 看到了一些示例,在这些示例中,您可以分别通过XAML声明哪一列是只读的,但这些是手动输入的预定数据。 I am trying to fill the DataGrid using Auto generated columns and data is coming from a database and I want to do it programmatically. 我正在尝试使用自动生成的列填充DataGrid,并且数据来自数据库,我想以编程方式进行操作。

Connection connection = new Connection();
public static DataTable dt = new DataTable();

private void FillTable(DataGrid dataGrid)
{
    //Connecting to database
    connection.ConnectToDB(table, dt);
    dataGrid.ItemsSource = dt.DefaultView;

    foreach (DataGridColumn column in dataGrid1.Columns)
    {
        if (column.DisplayIndex == 1)
        {
            dataGrid1.IsReadOnly = true;
        }              
    }

    //dataGrid1.IsReadOnly = true;
}

XAML: XAML:

<DataGrid x:Name="dataGrid1" Canvas.Left="10" Canvas.Top="10" Height="auto" Width="auto" MaxHeight="400" AutoGenerateColumns="True" Style="{DynamicResource DataGridStyle1}"/>

If columns are auto generated then handle DataGrid.AutoGeneratingColumn . 如果自动生成列,则处理DataGrid.AutoGeneratingColumn

private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyName != "Participants")
    { e.Column.IsReadOnly = true; }
}

And change FillTable method: 并更改FillTable方法:

private void FillTable(DataGrid dataGrid)
{
    connection.ConnectToDB(table, dt);
    dataGrid.ItemsSource = dt.DefaultView;
}

Set the AutoGenerateColumns -Property to false, than you can provide the properties you want in the visual-design you want. AutoGenerateColumns -Property设置为false,然后可以在所需的视觉设计中提供所需的属性。

Small example with skinning: 蒙皮的小例子:

<GroupBox Grid.Row="2" Header="Items" Padding="5">
    <GroupBox.Resources>
        <Style TargetType="{x:Type DataGrid}">
            <Style.Resources>
                <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#C2E58F" />
                <Style TargetType="{x:Type DataGridCell}">
                    <Style.Triggers>
                        <Trigger Property="IsSelected" Value="True">
                            <Setter Property="Background" Value="#C2E58F" />
                            <Setter Property="Foreground" Value="Black" />
                            <Setter Property="BorderThickness" Value="0" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Style.Resources>
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="AutoGenerateColumns" Value="False" />
            <Setter Property="AlternationCount" Value="2" />
            <Setter Property="AlternatingRowBackground" Value="LightGray" />
            <Setter Property="CanUserAddRows" Value="False" />
            <Setter Property="CanUserDeleteRows" Value="False" />
            <Setter Property="SelectionMode" Value="Single" />
            <Setter Property="SelectionUnit" Value="FullRow" />
            <Setter Property="FocusManager.IsFocusScope" Value="True" />
        </Style>
    </GroupBox.Resources>
    <DataGrid ItemsSource="{Binding NameNotes, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
              AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Firstname" Width="Auto" SortMemberPath="Firstname">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Firstname}" VerticalAlignment="Center" Margin="3,1"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
                <DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <TextBox Text="{Binding Firstname}" VerticalAlignment="Center" Margin="3,1"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellEditingTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Lastname" Width="Auto" SortMemberPath="Lastname">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Lastname}" VerticalAlignment="Center" Margin="3,1"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
            <DataGridTemplateColumn Header="Note" Width="*" SortMemberPath="Note">
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Note}" VerticalAlignment="Center" Margin="3,1" TextWrapping="WrapWithOverflow"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>
</GroupBox>

To edit one column you have to provide a CellEditingTemplate for this column. 要编辑一个列,您必须为此列提供一个CellEditingTemplate I've done this for the Firstname-Column in my example. 在我的示例中,我已经完成了Firstname-Column的操作。

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

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