2016-03-20 15 views
0

リストとアイテムソースを使用して動的にデータグリッドに行を追加しています。WPFで動的に追加するときの編集を無効にする

しかし、データグリッドの一部のセルを編集するためにユーザーを無効にしたいとします。

私は簡単な方法でこれを行うことができますか?

添付私のコード:あなたがしたい場合

/// <summary> 
    /// this class contain the data for the table in order to add the data for the table 
    /// </summary> 
    public class DataFroGrid 
    { 
     private string des; 
     /// <summary> 
     /// conatin the desction field for the data 
     /// </summary> 
     public string Description 
     { 
      get { return des; } 
      set { des = value; } 
     } 
     private string numberOfBytes; 
     /// <summary> 
     /// contain the number of byte for adding to the data 
     /// </summary> 
     public string NumberOfBytes 
     { 
      get { return numberOfBytes; } 
      set { numberOfBytes = value; } 
     } 

     private string value; 

     /// <summary> 
     /// contain the value for adding to the data 
     /// </summary> 
     public string Value 
     { 
      get { return this.value; } 
      set { this.value = value; } 
     } 

     public DataFroGrid() 
     { 
      des = ""; 
      numberOfBytes = ""; 
      value = ""; 
     } 
    } 

    private List<DataFroGrid> _ListForDataCommands; // a list for attached the data as a data source 

    public addQuestionMarkCommand(string[] description, int[] numberOfBytes ,string [] Value) 
    { 
     WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen; // start window in the middle of the screen 
     _ListForDataCommands = new List<DataFroGrid>(); 
     res = ""; // get the result values 
     InitializeComponent();    
     eUserClosed += addQuestionMarkCommand_eUserClosed; // create an event for closing the window. 

     // update the item source per the data that has been received 
     for (int i = 0; i < description.Length; i++) 
     { 
      DataFroGrid dfg = new DataFroGrid(); 
      dfg.Description = description[i]; 
      dfg.NumberOfBytes = numberOfBytes[i].ToString(); 
      dfg.Value = Value[i]; 
      _ListForDataCommands.Add(dfg);     
      //want to disable editing cell per data???? 
     } 

     dataGridCommand.ItemsSource = _ListForDataCommands;            
} 
+0

あなたのXAMLを見ることが有用であろう。これをチェックして他のアイデアを投稿してください:http://stackoverflow.com/questions/3843331/how-to-make-wpf-datagridcell-readonly – Taterhead

答えて

0

コードビハインドソリューション、DataGridColumnIsReadOnlyプロパティがあり、あなたがDataGrid.AutoGeneratingColumnイベントのイベントハンドラでそれを設定することができます。参考になるMSDN link for AutoGeneratingColumn eventがあります。

はここNumberOfBytes列が読み取り専用にするコードスニペットです:

private void Grid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) 
{ 
    if (e.Column.Header.ToString() == "NumberOfBytes") 
    {     
     e.Column.IsReadOnly = true; // Makes the column as read only 
    } 
} 
+0

しかし、私はいくつかの行にいくつかのセルを欲しいですか? –

0

DataGridCellは、プロパティでIsEnabledがあります。 CellStyleをオーバーライドし、IsEnabledプロパティにバインディングを追加することができます。

<DataGrid> 
     <DataGrid.CellStyle> 
      <Style TargetType="DataGridCell"> 
       <Setter Property="IsEnabled" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Converter={local:IsEnabledConverter}}" /> 
      </Style> 
     </DataGrid.CellStyle> 
</DataGrid> 

決定はIsEnabledConverterになされるべきである。

public class IsEnabledConverter : MarkupExtension, IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     var cell = value as DataGridCell; 
     var rowData = cell.DataContext as DataFroGrid; // data object bound to the row 

     // you may analyze column info, row data here and make a decision 

     if (cell.Column.Header.ToString()=="ColumnName1") 
     {     
      return false; 
     } 

     return true; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotSupportedException(); 
    } 

    public override object ProvideValue(IServiceProvider serviceProvider) 
    { 
     return this; 
    } 
} 
関連する問題