使用MVVM在WPF中绑定失败

时间:2014-05-23 11:13:31

标签: c# wpf mvvm binding avalonedit

我创建了一个继承自AvalonEdit的自定义TextEditor控件。我这样做是为了方便使用这个编辑器控件来使用MVVM和Caliburn Micro。 [为了显示目的而减少] MvvTextEditor类是

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,但我们只考虑现在的长度,因为问题是相同的)。如果我用鼠标选择了某些东西,从View到View模型的绑定效果很好。现在,我编写了一个查找和替换实用程序,我想从后面的代码中设置SelectionLengthget控件中有setTextEditor)。在我的视图模型中,我只是设置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本身[它呈现了它自己的问题]吗

感谢您的时间。

3 个答案:

答案 0 :(得分:1)

这是因为Getter中的SetterDependencyProperty只是.NET Wrapper。该框架将使用GetValueSetValue本身。

您可以尝试从PropertyChangedCallback访问DependencyProperty并设置正确的值。

 public int SelectionLength
        {
            get { return (int)GetValue(SelectionLengthProperty); }
            set { SetValue(SelectionLengthProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectionLength.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectionLengthProperty =
            DependencyProperty.Register("SelectionLength", typeof(int), typeof(MvvmTextEditor), new PropertyMetadata(0,SelectionLengthPropertyChanged));


        private static void SelectionLengthPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {
            var textEditor = obj as MvvmTextEditor;

            textEditor.SelectionLength = e.NewValue;
        }

答案 1 :(得分:1)

如果您仍然打开,这是另一个答案。由于SelectionLength已经被定义为基类的依赖属性,而不是创建派生类(或者将已存在的属性添加到派生类),我将使用附加属性来实现相同的功能。

关键是使用System.ComponentModel.DependencyPropertyDescriptor来订阅已存在的SelectionLength依赖项属性的change事件,然后在事件处理程序中执行所需的操作。

以下示例代码:

public class SomeBehavior
{
    public static readonly DependencyProperty IsEnabledProperty
        = DependencyProperty.RegisterAttached("IsEnabled",
        typeof(bool), typeof(SomeBehavior), new PropertyMetadata(OnIsEnabledChanged));

    public static void SetIsEnabled(DependencyObject dpo, bool value)
    {
        dpo.SetValue(IsEnabledProperty, value);
    }

    public static bool GetIsEnabled(DependencyObject dpo)
    {
        return (bool)dpo.GetValue(IsEnabledProperty);
    }

    private static void OnIsEnabledChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs args)
    {
        var editor = dpo as TextEditor;
        if (editor == null)
            return;

        var dpDescriptor = System.ComponentModel.DependencyPropertyDescriptor.FromProperty(TextEditor.SelectionLengthProperty,editor.GetType());
        dpDescriptor.AddValueChanged(editor, OnSelectionLengthChanged);
    }

    private static void OnSelectionLengthChanged(object sender, EventArgs e)
    {
        var editor = (TextEditor)sender;
        editor.Select(editor.SelectionStart, editor.SelectionLength);
    }
}

下面的Xaml:

  <Controls:TextEditor Behaviors:SomeBehavior.IsEnabled="True">
    </Controls:TextEditor>

答案 2 :(得分:0)

这就是我这样做的方式......

public static readonly DependencyProperty SelectionLengthProperty =
     DependencyProperty.Register("SelectionLength", typeof(int), typeof(MvvmTextEditor),
     new PropertyMetadata((obj, args) =>
         {
             MvvmTextEditor target = (MvvmTextEditor)obj;
             if (target.SelectionLength != (int)args.NewValue)
             {
                 target.SelectionLength = (int)args.NewValue;
                 target.Select(target.SelectionStart, (int)args.NewValue);
             }
         }));

public new int SelectionLength
{
    get { return base.SelectionLength; }
    //get { return (int)GetValue(SelectionLengthProperty); }
    set { SetValue(SelectionLengthProperty, value); }
}

对不起,浪费时间。我希望这有助于其他人...