我可以指定 WPF DataGrid 中哪些列是可编辑的吗?

发布于 2024-10-08 06:56:53 字数 173 浏览 3 评论 0原文

我有一个带有自动生成列的 WPF 4.0 DataGrid。我只想允许用户编辑第一列。有一个简单的方法可以做到这一点吗?

我试图添加 DataGridCell 样式并根据 ColumnName(第一列始终具有相同的名称)或 ColumnIndex 设置其编辑能力,但是我无法找出正确的 XAML,或者即使这是可能的。

I have a WPF 4.0 DataGrid with AutoGenerated columns. I would like to only allow the user to edit the first column. Is there an easy way of doing this?

I was trying to add a DataGridCell style and set it's editing ability based on either ColumnName (1st column always has the same name) or ColumnIndex, however I cannot figure out the correct XAML for this, or even if it is possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

歌入人心 2024-10-15 06:56:53

下面的示例可以解决一列或多列的问题

  private void Grid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        if (e.Column.Header.ToString() == "COLUMNNAME")
        {
            // e.Cancel = true;   // For not to include 
            // e.Column.IsReadOnly = true; // Makes the column as read only
        }

    } 

The below sample does the trick for one or more columns

  private void Grid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
    {
        if (e.Column.Header.ToString() == "COLUMNNAME")
        {
            // e.Cancel = true;   // For not to include 
            // e.Column.IsReadOnly = true; // Makes the column as read only
        }

    } 
水晶透心 2024-10-15 06:56:53

每列都有一个 IsReadOnly 属性。另外,整个 DataGrid 也具有 IsReadOnly [这不会影响绑定,只会影响用户编辑字段的能力]

编辑:
急于回复,抱歉。我只能猜测,如果可能的话,您不应该自动生成列,因此您可以控制哪些列是只读的以及哪个控制模板位于何处...只需使用列的 Binding 属性,这样您就不需要自动生成它们。

Each column has a IsReadOnly Property. Also, the whole DataGrid has the IsReadOnly as well [This does NOT affect the binding, just the ability of the user to edit the field]

EDIT:
Rushed an answer, sorry. I can only guess that you should NOT auto-generate columns if possible, so you can control which ones are readonly and which controltemplate goes where... just use the Binding property of the columns so you dont need to autogenerate them.

待天淡蓝洁白时 2024-10-15 06:56:53

我明白了......这就是我使用的:

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="IsEnabled" Value="False" />
        <Style.Triggers>
            <DataTrigger Value="PART_IsSelected" Binding="{Binding Path=Column.Header, RelativeSource={RelativeSource Self}}">
                <Setter Property="IsEnabled" Value="True" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.Resources>

如果你愿意,你可以使用 Column.DisplayIndex 代替 Column.Header

我仍然不确定为什么绑定不能直接工作并且需要由相对源引用,但至少它有效:)

I got it.... here's what I used:

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridCell}">
        <Setter Property="IsEnabled" Value="False" />
        <Style.Triggers>
            <DataTrigger Value="PART_IsSelected" Binding="{Binding Path=Column.Header, RelativeSource={RelativeSource Self}}">
                <Setter Property="IsEnabled" Value="True" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</DataGrid.Resources>

If you want, you can use Column.DisplayIndex instead of Column.Header

I'm still not sure why the binding won't work directly and needs to be referenced by a RelativeSource, but at least it works :)

羁客 2024-10-15 06:56:53
private void dgTableDetailAdj_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
    foreach (DataGridColumn col in dgTableDetailAdj.Columns)
    {
        if (col.Header.Equals("columnName"))
        {
            col.IsReadOnly = true;
        }
    }
}
private void dgTableDetailAdj_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
    foreach (DataGridColumn col in dgTableDetailAdj.Columns)
    {
        if (col.Header.Equals("columnName"))
        {
            col.IsReadOnly = true;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文