wpf 如何在只读数据网格中使一列可编辑?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14815951/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 07:28:04  来源:igfitidea点击:

How to make one column editable in a readonly datagrid?

wpfdatagrid

提问by sony

How to make one column editable in a readonly datagrid?

如何在只读数据网格中使一列可编辑?

<DataGrid x:Name="dgLoadDtl" Height="315" Width="710" Grid.Row="0" 
                  HorizontalAlignment="Left" VerticalAlignment="Bottom"  
                  Style="{DynamicResource StyleDatagrid}" 
                  IsReadOnly="true">

            <DataGrid.Columns>                    

                <DataGridTextColumn Foreground="Black" Width="60" Header="Sctn" Binding="{Binding Sctn, Mode=TwoWay}" IsReadOnly="false" />                    
                <DataGridTextColumn Foreground="Black" Width="140" Header="CustName"  Binding="{Binding CustName, Mode=TwoWay}" />
                <DataGridTextColumn Foreground="Black" Width="140" Header="Address"  Binding="{Binding Address1, Mode=TwoWay}" />
                <DataGridTextColumn Foreground="Black" Width="50" Header="Bulk   or Bag"  Binding="{Binding BulkorBag, Mode=TwoWay}" />
                <DataGridTextColumn Foreground="Black" Width="80" Header="ProdCode" Binding="{Binding ProdCode, Mode=TwoWay}" />
                <DataGridTextColumn Foreground="Black" Width="80" Header="MedCode" Binding="{Binding MedCode, Mode=TwoWay}" />

回答by Jon Heaton

I created a sample where I bound the ItemsSource of the DataGrid to an ObservableCollection and from here you have two options.

我创建了一个示例,将 DataGrid 的 ItemsSource 绑定到 ObservableCollection,从这里您有两个选择。

  1. Set AutoGenerateColumns="False" on the DataGrid and set IsReadOnly="True" for all columns except the column you want to be editable you will set IsReadOnly="False".
  2. AutoGenerateColumns="True" (it is the default, so you could just remove the attribute from the XAML) and make the setters private in your ViewModel for all of the properties except the column you want to be editable.
  1. 在 DataGrid 上设置 AutoGenerateColumns="False" 并为除要编辑的列之外的所有列设置 IsReadOnly="True" 您将设置 IsReadOnly="False"。
  2. AutoGenerateColumns="True"(它是默认值,因此您可以从 XAML 中删除该属性)并在您的 ViewModel 中为所有属性设置私有的 setter,除了您要编辑的列。

Here is my sample code for option 1:

这是我的选项 1 的示例代码:

<DataGrid x:Name="dgLoadDtl" Grid.Row="0" HorizontalAlignment="Left" VerticalAlignment="Top" ItemsSource="{Binding MyData}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Foreground="Black" Width="60" Header="Sctn" Binding="{Binding Sctn, Mode=TwoWay}" IsReadOnly="false" />
        <DataGridTextColumn Foreground="Black" Width="140" Header="CustName"  Binding="{Binding CustName, Mode=TwoWay}" IsReadOnly="True"/>
        <DataGridTextColumn Foreground="Black" Width="140" Header="Address"  Binding="{Binding Address1, Mode=TwoWay}" IsReadOnly="True"/>
        <DataGridTextColumn Foreground="Black" Width="50" Header="Bulk   or Bag"  Binding="{Binding BulkorBag, Mode=TwoWay}" IsReadOnly="True"/>
        <DataGridTextColumn Foreground="Black" Width="80" Header="ProdCode" Binding="{Binding ProdCode, Mode=TwoWay}" IsReadOnly="True"/>
        <DataGridTextColumn Foreground="Black" Width="80" Header="MedCode" Binding="{Binding MedCode, Mode=TwoWay}" IsReadOnly="True"/>
    </DataGrid.Columns>
</DataGrid>