2013-02-11 92 views
6

如何使一列在只讀數據網格中可編輯?如何在只讀數據網格中編輯一列?

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

+6

您必須刪除'IsReadyOnly =「真正的」'從主'DataGrid'並對被只讀你要添加的每個列'IsReadOnly =「true」''比如'' – 2013-02-11 16:17:33

+0

/\不起作用。 – Kirka121 2014-03-10 15:12:20

+0

檢查了這一點: http://stackoverflow.com/questions/4471934/can-i-specify-which-columns-are-editable-in-a-wpf-datagrid – 2014-06-28 12:22:19

回答

10

我創造,我必然的DataGrid的ItemsSource到一個ObservableCollection樣本,並從這裏你有兩個選擇。

  1. 設置的AutoGenerateColumns在DataGrid中=「假」,並設置IsReadOnly =「真」爲你想編輯您將設置IsReadOnly =「假」,除了列中的所有列。
  2. AutoGenerateColumns =「True」(它是默認值,因此您可以從XAML中刪除該屬性),並使您的ViewModel中的setter專用於除想要編輯的列之外的所有屬性。

這裏是選擇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> 
相關問題