如何在不使用MVVM的情况下绑定DependencyProperty

3

我正在做一个小项目,发现没有必要实现完整的MVVM。

我试图在代码后台中绑定一些属性,但无法使其正常工作。

关键是使用DependencyProperties和代码后台中的绑定。

我尝试遵循以下链接和SO中的问题:

在WPF中的CodeBehind中绑定Dependency Property

如何:在代码中创建绑定

将在Code-Behind中定义的Dependency Property通过Xaml绑定到UserControl的DataContext属性

但它们都与MVVM有关,或者至少我不能在我的情况下调整代码。

这个例子应该非常简单。

MainWindow.xaml

<Label Name="_lblCurrentPath"
        Style="{StaticResource CustomPathLabel}"
        ToolTip="{Binding CurrentPath}"
        Content="{Binding CurrentPath, Mode=TwoWay,
                     UpdateSourceTrigger=PropertyChanged}"/>

MainWindow.xaml.cs

public MainWindow()
{
    InitializeComponent();
    SetBindings();
}

#region Properties

public static readonly DependencyProperty CurrentPathProperty =
    DependencyProperty.Register("CurrentPath", typeof(String), typeof(MainWindow), new PropertyMetadata(String.Empty, OnCurrentPathChanged));


public string CurrentPath
{
    get { return (String)GetValue(CurrentPathProperty); }
    set { SetValue(CurrentPathProperty, value); }
}


#endregion

#region Bindings

private void SetBindings()
{
    // Label CurrentPath binding
    Binding _currentPath = new Binding("CurrentPath");
    _currentPath.Source = CurrentPath;
    this._lblCurrentPath.SetBinding(Label.ContentProperty, _currentPath);
}

#endregion

#region Methods

private void Refresh()
{
    MessageBox.Show("Refresh!");
}

private string Search()
{
    WinForms.FolderBrowserDialog dialog = new WinForms.FolderBrowserDialog();

    WinForms.DialogResult _dResult = dialog.ShowDialog();

    switch(_dResult)
    {
        case WinForms.DialogResult.OK:
            CurrentPath = dialog.SelectedPath;
            break;
        default:
            break;
    }

    return CurrentPath;
}

#endregion

#region Events

private static void OnCurrentPathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    MainWindow instance = d as MainWindow;
    instance.Refresh();
}

public void OpenSearchEclipsePath(object sender, RoutedEventArgs e)
{
    CurrentPath = Search();
}

public void RefreshEclipsePath(object sender, RoutedEventArgs e)
{
    Refresh();
}

你有什么想法吗?

如果这是不好的实践,我应该使用MVVM,欢迎评论。

此外...与Command属性相关。在这种情况下,如果我不想使用MVVM方法,注册事件是否更好?我发现使用自定义命令绑定有点麻烦。


1
这是一个不好的实践,你应该使用MVVM(你说我们可以评论;))。 - BradleyDotNET
1个回答

3

首先,您可以完全在不使用MVVM的情况下使用绑定。我不建议这样做,因为当您使用MVVM时代码更加整洁,但是这是可以实现的。您只需要在构造函数中加入以下代码即可:

this.DataContext = this;

现在你的视图也是你的视图模型!就像我说的,这不是一个好主意。
现在,你的代码中在MainWindow类中有一个DependencyProperty。不要那样做。它完全没有作用。DP存在的目的是让父控件可以对它们进行绑定。 MainWindow没有父级,所以DP是无用的。
你只需要设置一个常规属性即可:
public string CurrentPath
{
    get { return currentPath; }
    set
    {
         currentPath = value;
         NotifyPropertyChanged();
    }
}

接下来让 MainWindow 实现 INotifyPropertyChanged 接口(我是否提到使用一个简单的视图模型更有意义?)。

回答你关于 Command 的问题。是的,如果你反对使用命令,只需注册事件即可。但是,Command 是一种非常好的方式,可以将用户点击传递给视图模型,而不会破坏 MVVM 模式。语法并不是很糟糕。如果你已经采用了“视图作为视图模型”的方法,那么 Command 并没有太多用处。


谢谢。这很简单。是的,最后我想我会采用MVVM的方法。以前我习惯自己创建它们,但我发现了一些很好的框架,比如mvvmlight或caliburn。 - blfuentes
@blacai 尽管继续使用你喜欢的框架,但对我来说,为每个新项目建立自己的框架只需要不到五分钟。这真的不难。很高兴听到你打算以正确的方式进行! - BradleyDotNET
目前我没有最喜欢的框架。就像我说的,我习惯自己创建框架,但是我看到很多关于它们的问题,看看也不错 :) - blfuentes
1
@blacai 嗯,98%的时间框架会让我的生活更加困难,所以我尽量避免使用它们,除非我真的需要。祝你在寻找中好运。 - BradleyDotNET

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接