XML 元素不会触发 TextBox ConvertBack 事件 [英] TextBox ConvertBack event doesn't fire for XML element

查看:24
本文介绍了XML 元素不会触发 TextBox ConvertBack 事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ValueFormattingConverter.Convert 使用 XmlElement 调用.ConvertBack 永远不会被调用.为什么?是否有义务将绑定指令向下传递?TextBox 的使用是否覆盖了它自己的绑定设置?可以做什么?

ValueFormattingConverter.Convert is called with the XmlElement. ConvertBack is never called. Why? Is there some obligation to pass binding directives down the chain? Is the use of the TextBox overriding its own binding settings? What can be done?

我的文本框

<TextBox Width="200" 
         Text="{Binding Path=., Converter={StaticResource valueFormattingConverter}}",
         Mode=TwoWay,
         NotifyOnSourceUpdated=True,
         NotifyOnValidationError=True,                
         UpdateSourceTrigger=PropertyChanged}" />

用法相当复杂.从顶部开始,我们为选项卡提供了一个 XML 元素.

Usage is rather convoluted. Starting at the top, we provide an XML element to a tab.

<TabItem.DataContext>
    <Binding Source="{StaticResource mcf}", 
             XPath="mdf/press_information"/>
</TabItem.DataContext>

该选项卡包含一个 ItemsControl,它通过传递绑定的 ControlChooser 构建 TextBoxes.

That tab contains a ItemsControl which builds TextBoxes through this ControlChooser which passes the binding along.

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <W3V:ControlChooser RelativeSource="{RelativeSource AncestorType=W3V:ObjectList}", 
                            Content="{Binding}"/>
    </DataTemplate>
</ItemsControl.ItemTemplate>

我的转换器类标题.调用转换方法.从不转换回.

My converter class header. Convert method is called. ConvertBack never.

  [ValueConversion(typeof(XmlElement), typeof(string))]
  public class ValueFormattingConverter : IValueConverter

所选答案基本上是 Path=.不支持2路绑定.我相信这是问题的正确答案.知道这很有帮助,但不能那样做"并不能解决更大的问题.所以我在这里提出了更大的问题:生成可编辑表单的方法来自 XML.

The chosen answer basically says Path=. doesn't support 2-way binding. I believe it is the correct answer to the question. Very helpful to know, but "can't do that" doesn't solve the larger problem. So I have laid out the larger question here: Means of generating an editable form from XML.

推荐答案

到源的绑定方向不适用于 {Binding Path=.}.这是因为没有绑定属性,只有绑定源对象.

The binding direction to source won't work with a {Binding Path=.}. This is because there is no bound property, but just the binding source object.

因此永远不会有源更新,并且永远不会调用 ConvertBack 方法,因为这意味着替换源对象.

Hence there will never be a source update, and the ConvertBack method is never called, because that would mean to replace the source object.

为了使您的代码工作,您必须绑定到某些属性:

In order to make your code work, you would have to bind to some property:

<TextBox Text="{Binding Path=SomeElement, ...}"/>

这篇关于XML 元素不会触发 TextBox ConvertBack 事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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