简体   繁体   English

带有自定义设置器的 Winforms 数据绑定

[英]Winforms Databinding with a custom setter

I'm using some good old fashing DataBinding in a Winforms project.我在 Winforms 项目中使用了一些很好的旧式 DataBinding。

I have my form with a control (A devExpress RichTextEdit for those that want to know)我有一个带有控件的表单(对于那些想知道的人来说是一个 devExpress RichTextEdit)

I want to bind the HtmlText property of the richTextEdit control to a property on my ViewModel我想将 RichTextEdit 控件的 HtmlText 属性绑定到我的 ViewModel 上的一个属性

I have done that binding and that is not a problem.我已经完成了绑定,这不是问题。 However I have realised that the HtmlText that comes out of the richTextEdit is HtmlEncoded.但是我已经意识到从richTextEdit 出来的HtmlText 是HtmlEncoded。 Meaning that characters get encoded into their html entity representation.这意味着字符被编码到它们的 html 实体表示中。 eg < becomes &lt;例如<变成&lt; etc等等

I don't want this to happen as those tags have special meaning further down the line and I need to keep them.我不希望这种情况发生,因为这些标签具有特殊的意义,我需要保留它们。

So in my ViewModel that has all the notify property changed stuff and essentially wraps my domain object I could do this所以在我的 ViewModel 中,所有的通知属性都发生了变化,并且基本上包装了我的域对象,我可以这样做

public class ViewModel: INotifyPropertyChanged
{
    public string WrappedProperty
    {
        get => domainObject.Property;
        set
        {
            domainObject.Property = HttpUtility.DecodeHtml(value);
            //Raise Property changed event etc
        }
    }
}

and in my form I create a Data binding在我的表单中我创建了一个数据绑定

Binding binding = new Binding("HtmlText", _viewModel, "WrappedProperty", true, DataSourceUpdateMode.OnPropertyChanged,null,null);
_richEditControl.DataBindings.Add(binding);

now this works as intended, however I don't like it.现在这按预期工作,但是我不喜欢它。 My view model is doing things because of the control I am currently using.由于我当前使用的控件,我的视图模型正在执行操作。 Its 'leaky' and it smells.它的“泄漏”和气味。

I want my View to be handle view specific issues.我希望我的视图能够处理视图特定的问题。

What I'd like to do is to create a binding between the controls Html Text property and my View models WrappedProperty property, providing a custom function to be used when setting the property from the control into the view model.我想要做的是在控件 Html Text 属性和我的视图模型 WrappedProperty 属性之间创建一个绑定,提供一个自定义函数,以便在将属性从控件设置到视图模型中时使用。 Is is something that can be implemented or is there some kind of common work around pattern that I am missing?是可以实现的东西还是我缺少的某种常见的围绕模式的工作?

Thanks谢谢

You can handle this in the binding using the Parse event.您可以使用Parse事件在绑定中处理此问题。

Binding binding = new Binding("HtmlText", _viewModel, "WrappedProperty", true, DataSourceUpdateMode.OnPropertyChanged,null,null);
binding.Parse += (sender, e) => e.Value = HttpUtility.DecodeHtml(e.Value);
_richEditControl.DataBindings.Add(binding);

I managed to discover this myself, but as I struggled to find anything for a while on google about this I thought'd I'd myself and hopefully help future developers我设法自己发现了这一点,但是当我努力在谷歌上找到关于此的任何内容时,我想我会自己并希望能帮助未来的开发人员

There is an event on a Binding called Parse.绑定上有一个名为 Parse 的事件。 Subscribing to this event allows to you to work with the value before it gets sent back to the data source.订阅此事件允许您在将值发送回数据源之前对其进行处理。

Its partner is the Format event this allows you to work the value before it is displayed in the control它的伙伴是 Format 事件,它允许您在该值显示在控件中之前对其进行处理

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.binding.parse?view=netframework-4.8 https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.binding.parse?view=netframework-4.8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM