そんなわけはありません。
と言ってみたけど検証不足でした。
そうかも。
前回は TextBox や Label から Slider への Binding でした。
でも実際使うとしたら Slider から データへの Binding です。
<Window x:Class="BindingTest.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Slider Name="slider1" Value="{Binding ElementName=textBox2,Path=Text,diag:PresentationTraceSources.TraceLevel=High}"/>
<TextBox Grid.Row="1" Name="textBox1" Text="{Binding ElementName=slider1,Path=Value,Mode=TwoWay,diag:PresentationTraceSources.TraceLevel=High}" />
<Label Grid.Row="2" Name="label1" Content="{Binding ElementName=slider1,Path=Value,diag:PresentationTraceSources.TraceLevel=High}"/>
<Button Grid.Row="3" Name="button1" Click="button1_Click">Button</Button>
<TextBox Grid.Row="4" Name="textBox2"/>
</Grid>
</Window>
slider1 から textBox2 に Binding させてみました。
1)依存関係プロパティーの値を変更 ボタンをクリックすれば slider1.Value = - 10 となる
System.Windows.Data Warning: 86 : BindingExpression (hash=41014879): Update - got raw value '-10'
System.Windows.Data Warning: 89 : BindingExpression (hash=41014879): Update - implicit converter produced '-10'
System.Windows.Data Warning: 90 : BindingExpression (hash=41014879): Update - using final value '-10'
System.Windows.Data Warning: 98 : BindingExpression (hash=41014879): SetValue at level 0 to TextBox (hash=34340385) using DependencyProperty(Text): '-10'
System.Windows.Data Warning: 92 : BindingExpression (hash=32176063): Got PropertyChanged event from Slider (hash=3888474) for Value
System.Windows.Data Warning: 97 : BindingExpression (hash=32176063): GetValue at level 0 from Slider (hash=3888474) using DependencyProperty(Value): '0'
System.Windows.Data Warning: 76 : BindingExpression (hash=32176063): TransferValue - got raw value '0'
System.Windows.Data Warning: 80 : BindingExpression (hash=32176063): TransferValue - implicit converter produced '0'
System.Windows.Data Warning: 85 : BindingExpression (hash=32176063): TransferValue - using final value '0'
System.Windows.Data Warning: 92 : BindingExpression (hash=24123405): Got PropertyChanged event from Slider (hash=3888474) for Value
System.Windows.Data Warning: 97 : BindingExpression (hash=24123405): GetValue at level 0 from Slider (hash=3888474) using DependencyProperty(Value): '0'
System.Windows.Data Warning: 76 : BindingExpression (hash=24123405): TransferValue - got raw value '0'
System.Windows.Data Warning: 85 : BindingExpression (hash=24123405): TransferValue - using final value '0'
SetValue at level 0 to TextBox (hash=34340385) using DependencyProperty(Text): '-10' TextBox に ? 10 をセットしています。
2)プロパティーの値を変える TextBox の値を 20 に変更すれば Mode=TwoWay で Slider.Value を変更する
System.Windows.Data Warning: 91 : BindingExpression (hash=32176063): Got LostFocus event from TextBox (hash=44150175)
System.Windows.Data Warning: 86 : BindingExpression (hash=32176063): Update - got raw value '20'
System.Windows.Data Warning: 89 : BindingExpression (hash=32176063): Update - implicit converter produced '20'
System.Windows.Data Warning: 90 : BindingExpression (hash=32176063): Update - using final value '20'
System.Windows.Data Warning: 98 : BindingExpression (hash=32176063): SetValue at level 0 to Slider (hash=3888474) using DependencyProperty(Value): '20'
System.Windows.Data Warning: 86 : BindingExpression (hash=41014879): Update - got raw value '20'
System.Windows.Data Warning: 89 : BindingExpression (hash=41014879): Update - implicit converter produced '20'
System.Windows.Data Warning: 90 : BindingExpression (hash=41014879): Update - using final value '20'
System.Windows.Data Warning: 98 : BindingExpression (hash=41014879): SetValue at level 0 to TextBox (hash=34340385) using DependencyProperty(Text): '20'
System.Windows.Data Warning: 92 : BindingExpression (hash=32176063): Got PropertyChanged event from Slider (hash=3888474) for Value
System.Windows.Data Warning: 97 : BindingExpression (hash=32176063): GetValue at level 0 from Slider (hash=3888474) using DependencyProperty(Value): '10'
System.Windows.Data Warning: 76 : BindingExpression (hash=32176063): TransferValue - got raw value '10'
System.Windows.Data Warning: 80 : BindingExpression (hash=32176063): TransferValue - implicit converter produced '10'
System.Windows.Data Warning: 85 : BindingExpression (hash=32176063): TransferValue - using final value '10'
System.Windows.Data Warning: 92 : BindingExpression (hash=24123405): Got PropertyChanged event from Slider (hash=3888474) for Value
System.Windows.Data Warning: 97 : BindingExpression (hash=24123405): GetValue at level 0 from Slider (hash=3888474) using DependencyProperty(Value): '10'
System.Windows.Data Warning: 76 : BindingExpression (hash=24123405): TransferValue - got raw value '10'
System.Windows.Data Warning: 85 : BindingExpression (hash=24123405): TransferValue - using final value '10'
SetValue at level 0 to TextBox (hash=34340385) using DependencyProperty(Text): '20' TextBox にやっぱり 20 を入れています。
しかし、問題点として、Slider の CoerceValueCallback のロジックと、textBox2(データソース側)の CoerceValueCallback のロジックが矛盾していたら困るので解決方法はないかも。
データソース側に CoerceValueCallback は通常つけないだろう(DependencyPropertyじゃないのが普通だし)がWPFの場合は両方ともコントロールの場合があるので起こりえるんです。