我创建了一个自定义的从AvalonEdit继承的TextEditor
控件。我这样做是为了方便使用此编辑器控件来使用MVVM和Caliburn Micro。 [剪切下来用于显示目的] MvvTextEditor
类是使用MVVM在WPF中绑定失败
public class MvvmTextEditor : TextEditor, INotifyPropertyChanged
{
public MvvmTextEditor()
{
TextArea.SelectionChanged += TextArea_SelectionChanged;
}
void TextArea_SelectionChanged(object sender, EventArgs e)
{
this.SelectionStart = SelectionStart;
this.SelectionLength = SelectionLength;
}
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;
}));
public new int SelectionLength
{
get { return base.SelectionLength; }
set { SetValue(SelectionLengthProperty, value); }
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged([CallerMemberName] string caller = null)
{
var handler = PropertyChanged;
if (handler != null)
PropertyChanged(this, new PropertyChangedEventArgs(caller));
}
}
现在,在持有该控制的观点,我有以下XAML:
<Controls:MvvmTextEditor
Caliburn:Message.Attach="[Event TextChanged] = [Action DocumentChanged()]"
TextLocation="{Binding TextLocation, Mode=TwoWay}"
SyntaxHighlighting="{Binding HighlightingDefinition}"
SelectionLength="{Binding SelectionLength,
Mode=TwoWay,
NotifyOnSourceUpdated=True,
NotifyOnTargetUpdated=True}"
Document="{Binding Document, Mode=TwoWay}"/>
我的问题是SelectionLength
(和SelectionStart
但让由于问题是相同的,我们只考虑现在的长度)。如果我用鼠标选择了一些东西,从视图到我的视图模型的绑定效果很好。现在,我已经编写了一个查找和替换实用程序,并且我想从后面的代码中设置SelectionLength
(其中get
和set
可用于TextEditor
控件)。在我的视图模型,我简单地设置SelectionLength = 50
,我在视图模型实现这个像
private int selectionLength;
public int SelectionLength
{
get { return selectionLength; }
set
{
if (selectionLength == value)
return;
selectionLength = value;
Console.WriteLine(String.Format("Selection Length = {0}", selectionLength));
NotifyOfPropertyChange(() => SelectionLength);
}
}
当我设置SelectionLength = 50
,则DependencyProperty SelectionLengthProperty
不会在MvvmTextEditor
类得到更新,它就像TwoWay
绑定到我的控制失败了,但使用Snoop没有这个迹象。我认为这只是通过绑定工作,但似乎并非如此。
有一些简单的我失踪,或将我不得不设立,并在MvvmTextEditor
类侦听改变我的视图模型的事件处理程序,并更新了DP本身[呈现它自身的问题]
谢谢你的时间。
我现在就试试这个,送还给你。感谢您的时间... – MoonKnight
您是否在构造函数中注册'SelectionLengthPropertyChanged'? – MoonKnight
不,我很抱歉。您正在将其注册到'Dependencyproperty''元数据'中。在我的例子中忘了这个。另外:尝试直接设置值到控制。这将是我猜测的最佳方式。 –