WPF Datagrid IsReadOnly绑定到当前行Item的属性

时间:2012-12-20 13:28:06

标签: wpf mvvm binding datagrid

我需要一些我的DataGrid行可编辑而其他行没有。

<DataGrid ItemsSource="{Binding Items.View}" AutoGenerateColumns="False" CanUserAddRows="False" >
    <DataGrid.Columns>
        <DataGridTextColumn IsReadOnly="True" Binding="{Binding Timestamp}" Width="Auto" >
            <DataGridTextColumn.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{lex:Loc DateTime}" />
                </DataTemplate>
            </DataGridTextColumn.HeaderTemplate>
        </DataGridTextColumn>
        <DataGridTextColumn Binding="{Binding Value}" Width="Auto" >
            <DataGridTextColumn.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Text="{lex:Loc Value}" />
                </DataTemplate>
            </DataGridTextColumn.HeaderTemplate>
        </DataGridTextColumn>

我的物品中有一个Property IsEditable。 我喜欢这样的东西:

<DataGridTextColumn IsReadOnly="{Binding IsEditable, Converter={StaticResource InverseBooleanConverter}}" Binding="{Binding Value}" Width="Auto" >

与此同时,我确实知道这不容易。虽然我找不到适合我的问题的解决方案。

更新

并不重要,但为了更好地理解,我将从viewmodel中添加更多数据。

private ObservableCollection<MeasuredValue> _last60values;
public ObservableCollection<MeasuredValue> Last60Values
{
    get { return _last60values; }
    set { _last60values = value; }
}

Items = new CollectionViewSource();
Items.Source = Last60Values;

和我的'MeasuredValue'

    namespace PQS.Model
    {
        public class MeasuredValue : CloneableBaseEntity<MeasuredValue>
        {

        public DateTime Timestamp { get; set; }

        .
        .
        .

        private double _value;
        [Range(0.0, double.MaxValue)]
        public double Value
        {
            get { return _value; }
            set
            {
                _value = value;
                RaisePropertyChanged("Value");
            }
        }

        private bool _isEditable;
        public bool IsEditable
        {
            get { return _isEditable; }
            set 
            {
                _isEditable = value;
                RaisePropertyChanged(() => IsEditable);
            }
        }

    }
}

0 个答案:

没有答案