【问题标题】:How to bind DependencyProperty without using MVVM如何在不使用 MVVM 的情况下绑定 DependencyProperty
【发布时间】:2014-12-31 19:09:49
【问题描述】:

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

我正在尝试在后面的代码中绑定一些属性,但无法使其工作。

重点是在后面的代码中使用 DependencyProperties 和 Binding。

我尝试在 SO 中关注这些链接和问题:

Bind Dependency Property in codebehind WPF

How to: Create a Binding in Code

Bind Dependency Property, defined in Code-Behind, through Xaml to a Property in the DataContext of a UserControl

但它们与 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,欢迎使用我们的 cmets。

.还...与Command 属性相关。在这种我不想使用 MVVM 方法的情况下,注册事件会更好吗?我发现使用自定义命令绑定有点乏味。

【问题讨论】:

  • 这是不好的做法,你应该使用 MVVM(你说我们可以评论;))。

标签: c# wpf xaml binding dependency-properties


【解决方案1】:

首先,您完全可以在没有 MVVM 的情况下使用绑定。我不会推荐它,因为当您使用 MVVM 时代码会更干净,但是可以做到。您需要做的就是将此行放入您的构造函数中:

this.DataContext = this;

现在您的视图也是您的视图模型!就像我说的那样,这不是一个好主意。

现在,您的代码在您的MainWindow 类中有一个DependencyProperty不要这样做。它完全没有任何目的。 DP 在那里,所以 parent 控件可以对它们进行绑定。 MainWindow 没有父级;所以DP没用。

您需要做的就是设置一个常规属性:

public string CurrentPath
{
    get { return currentPath; }
    set
    {
         currentPath = value;
         NotifyPropertyChanged();
    }
}

然后让MainWindow 实现INotifyPropertyChanged(我有没有提到使用简单的视图模型更有意义?)。

回答您的Command 问题。是的,如果您反对使用命令,只需注册活动即可。然而,Command 是一种在不破坏 MVVM 的情况下让用户点击进入视图模型的非常好的方法。语法并没有那么不好。但是,如果您仍然采用“视图作为视图模型”方法,Command 不会给您带来太多好处。

【讨论】:

  • 谢谢。很容易。是的,最后我想我会使用 MVVM 方法。我曾经自定义创建它们,但我发现了非常好的框架,如 mvvmlight 或 caliburn
  • @blacai 继续使用你最喜欢的框架,但是我需要花五分钟来为我做的每个新项目设置自己的框架。 真的并不难。很高兴听到你会以正确的方式做这件事!
  • 嗯,目前我没有最喜欢的框架。就像我说的,我曾经创建自己的,但我看到很多关于它们的问题,很高兴看看:)
  • @blacai Meh,98% 的时间框架让我的生活变得更加艰难,所以我尽量避免使用它们,除非我真的需要它们。祝您搜索顺利。
猜你喜欢
  • 1970-01-01
  • 2016-05-04
  • 1970-01-01
  • 2018-11-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-22
  • 2021-01-29
相关资源
最近更新 更多