我找到了一个很好的代码验证代码示例:
public class StringViewModel : ViewModelBase, IComparable
{
private string _value;
private string _originalValue;
public StringViewModel(string value)
{
_value = value;
_originalValue = value;
}
// Der eigentliche Wert.
public string Value
{
get { return _value; }
set
{
if (value != _value)
{
_value = value;
RaisePropertyChanged("Value");
HasChanges = _value != _originalValue && !(string.IsNullOrEmpty(_value) && string.IsNullOrEmpty(_originalValue));
}
}
}
public override void AcceptChanges()
{
_originalValue = _value;
HasChanges = false;
HasError = false;
}
public override void UndoChanges()
{
Value = _originalValue;
HasError = false;
}
// Muss implementiert werden, damit die Sortierung im CollectionView funktioniert.
public int CompareTo(object obj)
{
if (obj == null) return 1;
if (object.ReferenceEquals(this, obj)) return 0;
StringViewModel other = obj as StringViewModel;
return string.Compare(this._value, other._value);
}
public override string ToString()
{
return _value;
}
}
现在我想让它更通用,所以我需要在运行时创建下面的2个值:
private T? _value;
private T? _originalValue;
因为我想做类似的事情:
public class ItemModel : IComparable, INotifyEXTEND
{
private ??? _value;
private ??? _originalValue;
public ItemModel(string value)
{
_value = value;
_originalValue = value;
}
public ??? Value
{
get { return _value; }
set
{
if (value != _value)
{
_value = value;
RaisePropertyChanged("Value");
HasChanges = _value != _originalValue && !(string.IsNullOrEmpty(_value) && string.IsNullOrEmpty(_originalValue));
}
}
}
public override void AcceptChanges()
{
_originalValue = _value;
HasChanges = false;
HasError = false;
}
public override void UndoChanges()
{
Value = _originalValue;
HasError = false;
}
public int CompareTo(object obj)
{
if (obj == null) return 1;
if (object.ReferenceEquals(this, obj)) return 0;
??? other = obj as ???;
return string.Compare(this._value, other._value);
}
public override string ToString()
{
return _value;
}
}
所以我可以这样使用它:
public class PersonViewModel : ViewModelBase
{
private Person _person;
private ItemModel _lastname;
private ItemModel _birthdate;
private bool _isNew;
// Otherstuff
}
有关如何执行此操作的任何建议? (一些示例代码片段或链接会很好)
答案 0 :(得分:4)
您需要使用泛型:
public class ItemModel<T> : IComparable<ItemModel<T>>, INotifyEXTEND where T : IComparable<T>
答案 1 :(得分:1)
我的解决方案在SLaks启发暗示之后(感谢你)我跳了它帮助某人
public class ValueModel<T> : IComparable
{
private object _value;
private object _originalValue;
public T Value
{
get { return (T)_value; }
set
{
if ((object)value != _value)
{
_value = value;
}
}
}
public ValueModel(T value)
{
_value = value;
_originalValue = value;
}
public void AcceptChanges()
{
_originalValue = _value;
}
public void UndoChanges()
{
Value = (T)_originalValue;
}
// Muss implementiert werden, damit die Sortierung im CollectionView funktioniert.
public int CompareTo(object obj)
{
if (obj == null) return 1;
if (object.ReferenceEquals(this, obj)) return 0;
ValueModel<T> other = obj as ValueModel<T>;
if (typeof(T) == typeof(String))
return string.Compare(this._value.ToString(), other._value.ToString());
else
return ((IComparable)_value).CompareTo(other._value);
}
}
您可以像使用
一样使用它public ValueModel<int> Id {get;set;}
执行类似
的方法Id.AcceptChanges();
并绑定它
TextBox Text="{Binding Id.Value}"