我正在尝试使带有MVVM模式的AvalonEdit正常工作,但我真的不知道该怎么做。我想将SelectionLength和SelectionStart绑定(bind)到我的ViewModel,以便在执行一些业务逻辑时可以访问这两个值。
我开始像这样创建DependencyProperties:
public class MvvmTextEditor : TextEditor, INotifyPropertyChanged
{
/// <summary>
/// A bindable Text property
/// </summary>
public new string Text
{
get { return base.Text; }
set {
base.Text = value;
RaisePropertyChanged();
}
}
/// <summary>
/// A bindable SelectionStart property
/// </summary>
public new int SelectionStart
{
get { return base.SelectionStart; }
set {base.SelectionStart = value; }
}
/// <summary>
/// A bindable SelectionLength property
/// </summary>
public new int SelectionLength
{
get { return base.SelectionLength; }
set { base.SelectionLength = value; }
}
/// <summary>
/// The bindable selection start property dependency property
/// </summary>
public static readonly DependencyProperty SelectionStartProperty =
DependencyProperty.Register("SelectionStart", typeof(int), typeof(MvvmTextEditor),
new PropertyMetadata((o, args) =>
{
var target = (TextEditor)o;
target.SelectionStart = (int)args.NewValue;
}));
/// <summary>
/// The bindable selection length property dependency property
/// </summary>
public static readonly DependencyProperty SelectionLengthProperty =
DependencyProperty.Register("SelectionLength", typeof(int), typeof(MvvmTextEditor),
new PropertyMetadata((o, args) =>
{
var target = (MvvmTextEditor) o;
target.SelectionLength = (int)args.NewValue;
Debug.WriteLine(target.SelectionLength);
}));
/// <summary>
/// The bindable text property dependency property
/// </summary>
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(MvvmTextEditor),
new PropertyMetadata((o, args) =>
{
var target = (MvvmTextEditor)o;
target.Text = (string)args.NewValue;
}));
/// <summary>
/// Raises a property changed event
/// </summary>
/// <param name="property">The name of the property that updates</param>
public void RaisePropertyChanged([CallerMemberName] string caller = "")
{
var handler = PropertyChanged;
if (handler != null)
PropertyChanged(this, new PropertyChangedEventArgs(caller));
}
public event PropertyChangedEventHandler PropertyChanged;
}
TextDependencyProperty工作正常,但是SelectionLength和SelectionStart无法正常工作。
我为SelectionChanged添加了一个事件处理程序(但是我在SetValue中做的不完全是:
public MvvmTextEditor()
{
TextArea.SelectionChanged += TextArea_SelectionChanged;
}
void TextArea_SelectionChanged(object sender, EventArgs e)
{
SetValue(SelectionStartProperty, SelectionStart);
SetValue(SelectionLengthProperty, SelectionLength);
}
选择现在正在工作,但是向后进行选择存在问题。在这种情况下,SelectionStart始终为0。如果到目前为止我所做的一切正确,那么我将创建一个逻辑,如果有人向后选择,它将转换正确的索引和长度。我是否必须在PropertyMetaDataDelegate中实现此逻辑?
最佳答案
是的,一旦我查看,原因就很清楚了。在支持SelectionStart
和SelectionEnd
的DP的属性的 setter 中,我们设置base.SelectionStart
和base.SelectionLength
,这将创建这些值的循环更新,并阻止从基类正确更新这些值。下面是更新的MVVM TextEditor类。
/// <summary>
/// Class that inherits from the AvalonEdit TextEditor control to
/// enable MVVM interaction.
/// </summary>
public class MvvmTextEditor : TextEditor, INotifyPropertyChanged
{
/// <summary>
/// Default constructor to set up event handlers.
/// </summary>
public MvvmTextEditor()
{
TextArea.SelectionChanged += TextArea_SelectionChanged;
}
/// <summary>
/// Event handler to update properties based upon the selection changed event.
/// </summary>
void TextArea_SelectionChanged(object sender, EventArgs e)
{
this.SelectionStart = SelectionStart;
this.SelectionLength = SelectionLength;
}
#region Text.
/// <summary>
/// Dependancy property for the editor text property binding.
/// </summary>
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(MvvmTextEditor),
new PropertyMetadata((obj, args) =>
{
MvvmTextEditor target = (MvvmTextEditor)obj;
target.Text = (string)args.NewValue;
}));
/// <summary>
/// Provide access to the Text.
/// </summary>
public new string Text
{
get { return base.Text; }
set { base.Text = value; }
}
/// <summary>
/// Return the current text length.
/// </summary>
public int Length
{
get { return base.Text.Length; }
}
/// <summary>
/// Override of OnTextChanged event.
/// </summary>
protected override void OnTextChanged(EventArgs e)
{
RaisePropertyChanged("Length");
base.OnTextChanged(e);
}
#endregion // Text.
#region Caret Offset.
/// <summary>
/// DependencyProperty for the TextEditorCaretOffset binding.
/// </summary>
public static DependencyProperty CaretOffsetProperty =
DependencyProperty.Register("CaretOffset", typeof(int), typeof(MvvmTextEditor),
new PropertyMetadata((obj, args) =>
{
MvvmTextEditor target = (MvvmTextEditor)obj;
target.CaretOffset = (int)args.NewValue;
}));
/// <summary>
/// Provide access to the CaretOffset.
/// </summary>
public new int CaretOffset
{
get { return base.CaretOffset; }
set { base.CaretOffset = value; }
}
#endregion // Caret Offset.
#region Selection.
/// <summary>
/// DependencyProperty for the TextEditor SelectionLength property.
/// </summary>
public static readonly DependencyProperty SelectionLengthProperty =
DependencyProperty.Register("SelectionLength", typeof(int), typeof(MvvmTextEditor),
new PropertyMetadata((obj, args) =>
{
MvvmTextEditor target = (MvvmTextEditor)obj;
target.SelectionLength = (int)args.NewValue;
}));
/// <summary>
/// Access to the SelectionLength property.
/// </summary>
public new int SelectionLength
{
get { return base.SelectionLength; }
set { SetValue(SelectionLengthProperty, value); }
}
/// <summary>
/// DependencyProperty for the TextEditor SelectionStart property.
/// </summary>
public static readonly DependencyProperty SelectionStartProperty =
DependencyProperty.Register("SelectionStart", typeof(int), typeof(MvvmTextEditor),
new PropertyMetadata((obj, args) =>
{
MvvmTextEditor target = (MvvmTextEditor)obj;
target.SelectionStart = (int)args.NewValue;
}));
/// <summary>
/// Access to the SelectionStart property.
/// </summary>
public new int SelectionStart
{
get { return base.SelectionStart; }
set { SetValue(SelectionStartProperty, value); }
}
#endregion // Selection.
/// <summary>
/// Implement the INotifyPropertyChanged event handler.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName] string caller = null)
{
var handler = PropertyChanged;
if (handler != null)
PropertyChanged(this, new PropertyChangedEventArgs(caller));
}
}
我希望这可以帮助其他人。
关于c# - 使用AvalonEdit SelectionStart,SelectionLength进行Mvvm绑定(bind),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21911439/