How to make one column editable in a readonly datagrid?

十年热恋 提交于 2021-02-06 10:46:12

问题


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}" />


回答1:


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

  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.

Here is my sample code for option 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>


来源:https://stackoverflow.com/questions/14815951/how-to-make-one-column-editable-in-a-readonly-datagrid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!