Xceed DataGrid的绑定列属性为viewmodel属性

时间:2014-01-10 15:46:20

标签: wpf data-binding mvvm xceed-datagrid

我使用AutoCreatedColumns

绑定到Xceed DataGridControl(Community Edition)
ObservableCollection<ItemViewModel> Items

我想根据viewmodel属性上的ReadOnly属性标记创建的列'Editable属性。

 class ItemViewModel : ViewModelBase {

     [Editable(false)] 
     public string Id { get; set; }

     string _comment;
     [Editable(true)]
     public string Comment {
         get { return _comment; }
         set {
              _comment = value;
              NotifyOfPropertyChanged(() => Comment);
         }
     // Other properties ...

 }

这可能吗?或者是否有另一种方法可以挂钩创建列以检查绑定的属性并以编程方式设置ReadOnly?

1 个答案:

答案 0 :(得分:1)

我认为最佳解决方案就是连接ItemsSourceChangeCompleted这样的事件:

void _dataGrid_ItemsSourceChangeCompleted(object sender, EventArgs e)
{
    DataGridControl control = (DataGridControl)sender;
    Type itemType = control.ItemsSource.GetType().GenericTypeArguments[0];
    foreach (var col in control.Columns)
    {
        PropertyInfo propInfo = itemType.GetProperty(col.FieldName);
        if (propInfo != null)
        {
            EditableAttribute editableAttribute = propInfo.GetCustomAttributes().OfType<EditableAttribute>().FirstOrDefault();
            col.ReadOnly = (editableAttribute == null || !editableAttribute.Value);
        }
        else
        {
            col.ReadOnly = false;
        }
    }
}

或者,您可以按照here所述将单元格ReadOnly属性绑定到您的可编辑属性。

如果您知道要显示哪些列,可以简化上面的解决方案并绑定列ReadOnly属性,如下所示:

public class EditableConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null)
        {
            PropertyInfo propInfo = value.GetType().GenericTypeArguments[0].GetProperty(parameter.ToString());
            if (propInfo != null)
            {
                EditableAttribute editableAttribute = propInfo.GetCustomAttributes().OfType<EditableAttribute>().FirstOrDefault();
                return (editableAttribute == null || !editableAttribute.Value);
            }
        }
        return false;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
<xcdg:DataGridControl.Columns>
    <xcdg:Column FieldName="Id" 
                 ReadOnly="{Binding RelativeSource={RelativeSource Self}, 
                    Path=DataGridControl.ItemsSource, 
                    Converter={StaticResource EditableConverter}, ConverterParameter=Id}" />
    <xcdg:Column FieldName="Comment" 
                 ReadOnly="{Binding RelativeSource={RelativeSource Self}, 
                    Path=DataGridControl.ItemsSource, 
                    Converter={StaticResource EditableConverter}, ConverterParameter=Comment}" />
</xcdg:DataGridControl.Columns>

但是,您可以关闭AutoCreateColumns并在代码中自行定义Columns集合(或关闭AutoCreateItemProperties并创建您自己的DataGridCollectionViewSource适当地设置每个DataGridItemProperty.IsReadOnly