使用WPF动态添加动态时禁用编辑

时间:2016-03-20 13:50:14

标签: c# wpf datagrid

我使用list as和item source动态地向数据网格添加行。

但是我想禁用用户编辑数据网格的某些单元格。

我怎样才能以简单的方式做到这一点?

附上我的代码:

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

2 个答案:

答案 0 :(得分:0)

如果您需要代码隐藏解决方案,IsReadOnly上有DataGridColumn属性,您可以在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
    }
} 

答案 1 :(得分: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;
    }
}