Why does data binding break in OneWay mode?

被刻印的时光 ゝ 提交于 2019-11-30 03:31:06

问题


Here's a little XAML fragment. You will see

<StackPanel>
     <TextBox x:Name="txtValue">250</TextBox>
     <Slider x:Name="slide" 
             Value="{Binding ElementName=txtValue, Path=Text, 
                             Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" 
            Minimum="0" Maximum="500"></Slider>
</StackPanel>
  1. when you change the textbox value, the slider updates
  2. If you change the slider value explicitly, the previous behavior breaks a.k.a. stops working.

If I remove the Mode=OneWay set directive, (defaults to two-way) everything works perfectly.

Why is this happening?


回答1:


Use mode=TwoWay and set the UpdateSourceTrigger=Explicit.




回答2:


Your data binding is not broken but deactivated (http://en.wikipedia.org/wiki/Euphemism):

System.Windows.Data Warning: 75 : BindingExpression (hash=52697953): Deactivate
System.Windows.Data Warning: 99 : BindingExpression (hash=52697953): Replace item at level 0 with {NullDataItem}
System.Windows.Data Warning: 59 : BindingExpression (hash=52697953): Detach

Setting the trace level to high will produce this message in the VS output window in case you move the slider:

<Slider xmlns:trace="clr-namespace:System.Diagnostics;assembly=WindowsBase"
        Value="{Binding trace:PresentationTraceSources.TraceLevel=High,
            ElementName=txtValue, Path=Text, Mode=OneWay,
            UpdateSourceTrigger=PropertyChanged}"
        Minimum="0" Maximum="500"></Slider>


来源:https://stackoverflow.com/questions/1389038/why-does-data-binding-break-in-oneway-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!