是只是我,还是WPF混乱的数据绑定和定制的IValueConverters? [英] Is it just me, or is WPF a mess of databinding and custom IValueConverters?

查看:132
本文介绍了是只是我,还是WPF混乱的数据绑定和定制的IValueConverters?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

严重的是,似乎每次我想让我的UI元素相互交流时,我最终编写了一个新的,定制的IValueConverter :(有人告诉我,我做错了,请! p>

示例:




  • 只有在我的文本框包含有效的URI。很好,编码一个的时间UriIsValidConverter

  • 哦,哎呀,我也想在我处理时禁用它我想现在我需要编写一个 UriIsValidAndBoolIsFalseMultiConverter

  • 我想在某个目录中显示一个文件列表(我想我需要一个 DirectoryPathToFileList 转换器!

  • 哦,我想要每个图标$ code> FileInfoToBitmap 转换器的时间

  • 如果我的status-string包含错误,否则为绿色。Yay,我得到一个 StatusString的代码ToSolidColorBrushConverter



我真的认为这不比旧的Windows窗体方法好得多只需使用 TextChanged 事件(或任何)手动连接所有内容。我猜这还是一个选择。这是人们实际做的,也许,我正在努力尝试使一切适合数据绑定范式?



所以是的,请告诉我,如果这是真的WPF编码如何---或者如果我做错了,如果是这样,我应该怎么做。

解决方案

您的方法是完全有效的(尽管我将使用多线程的第二个例子,而不是一个这样一个专门的转换器),尽管通过将所有的逻辑放入XAML,您正在产生非常高的耦合应用程序的外观方式和行为方式,因为这样你可能需要查看MVVM模式来分离这些东西。



在MVVM模式下, XAML(视图)只包含非常简单的数据绑定到ViewModel中,它通过INotifyPropertyChanged接口处理所有逻辑并更新视图。第三个例子的代码可能如下所示:

  public class DirectoryManagerViewModel:INotifyPropertyChanged 
{
private string _directory;

public string Directory
{
get {reutrn _directory;
set
{
if(_directory!= value)
{
_directory = value;
OnPropertyChanged(Directory);
if(IsValidDirectory(value))
{
PopulateFiles();
}
}
}
}

public ObservableCollection< FileViewModel>文件{get;私人集合

private bool IsValidDirectory(string directory)
{
//如果目录存在则返回
}

private bool PopulateFiles ()
{
//填充目录中的文件
}
}

其中FileViewModel是包含文件的名称和图标的另一个视图模型。



这种方法的优点是ViewModels可以可以重复使用其他视图和其他技术,如ASP.NET或Winforms,这样您就不会被锁定到WPF堆栈中。 (如果你在设计师负责的环境中工作,负责行为的开发人员,这有助于定义这些边界)



在一天结束时虽然这个逻辑确实需要去某个地方,虽然有更好和更坏的方式构建你的应用程序,你仍然将编写一个字符串并将其转换成一系列文件名和图标的代码。


Seriously, it seems like every time I want to make my UI elements talk to each other, I end up coding a new, custom, IValueConverter :(. Someone tell me that I'm doing it wrong, please!

Examples:

  • I wanted a button to be enabled only if my textbox contained a valid URI. Great, time to code up a UriIsValidConverter!
  • Oh oops, I also wanted to disable it while I'm processing something. I guess now I need to code up a UriIsValidAndBoolIsFalseMultiConverter!
  • I want to display a list of files in a certain directory (specified by a textbox) inside a listbox. I guess I need a DirectoryPathToFileList converter!
  • Oh hey, I want icons for each of those files in the listview. Time for a FileInfoToBitmap converter!
  • I want my status to be red if my status-string contains "Error", and green otherwise. Yay, I get to code up a StatusStringToSolidColorBrushConverter!

I'm really thinking this isn't that much better than the old Windows Forms method of just wiring up everything manually using TextChanged events (or whatever). Which I guess is still an option. Is that what people actually do, perhaps, and I'm trying too hard to make everything fit into the databinding paradigm?

So yeah, please tell me if this is really how WPF coding is---or if I'm doing it wrong, and if so, what I should be doing.

解决方案

Your approach is perfectly valid (though I would use a multibinding for the second example, rather than a such a specialised converter), though by placing all your logic into the XAML you are producing very high coupling between the way the application looks and the way that it behaves, because of this you may want to look into the MVVM pattern to separate those things out.

Under the MVVM pattern your XAML (the view) just contains very simple data bindings into a ViewModel which handles all the logic and updates the view through the INotifyPropertyChanged interface. The code for your third example may look something like:

public class DirectoryManagerViewModel : INotifyPropertyChanged
{
    private string _directory;

    public string Directory
    {
        get { reutrn _directory; }
        set
        {
            if (_directory != value)
            {
                 _directory = value;
                 OnPropertyChanged("Directory");
                 if (IsValidDirectory(value))
                 {
                     PopulateFiles();
                 }
            }
        }
    }

    public ObservableCollection<FileViewModel> Files { get; private set; }

    private bool IsValidDirectory(string directory)
    {
          //return if the directory exists etc.
    }

    private bool PopulateFiles()
    {
         //Populate Files with files in directory
    }
}

Where FileViewModel is another view model which contains the name and the icon for a file.

The advantage of this approach is that the ViewModels can be reused with other views and other technologies such as ASP.NET or Winforms so you are not locked into the WPF stack. (alos if you work in an environment where there are designers responsible for the look and developers responsible for the behaviour, this helps define those boundaries)

At the end of the day though this logic does need to go somewhere and while there are better and worse ways to architect your application you are still going to be writing code that takes a string and converts it into a series of filenames and icons somewhere.

这篇关于是只是我,还是WPF混乱的数据绑定和定制的IValueConverters?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆