えムナウ Blog

えムナウ の なすがまま

目次

Blog 利用状況

ニュース


follow mnow at http://twitter.com


えムナウのプログラミングのページ

INETAJ

書庫

日記カテゴリ

ギャラリ

2009年6月2日 #

WPF データーバインディング CoerceValueCallback 問題かも

そんなわけはありません。

と言ってみたけど検証不足でした。

そうかも。

前回は 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の場合は両方ともコントロールの場合があるので起こりえるんです。

posted @ 22:42 | Feedback (12)

WPF データーバインディング は大丈夫です

データーバインディングの今後に考えてみた

添付プロパティーとプロパティーとバインドします。
1)添付プロパティーの値を変更→SetValue→プロパティーに値を渡す→CoerceValueCallbackで値が変わるときがある。
結果 添付プロパティーとプロパティーの値が違う
2)プロパティーの値を変える→PropertyChangedCallback→SetValue→CoerceValueCallbackで値が変わるときがある。
結果 添付プロパティーとプロパティーの値が違う

そんなわけはありません。

というわけで調べてみた。

<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 />
    </Grid.RowDefinitions>
    <Slider  Name="slider1" />
    <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>
  </Grid>
</Window>

添付プロパティと依存関係プロパティは強制値コールバックの実装には違いがないので依存関係プロパティで確認してみた。
今回は強制値コールバックを行っている Slider を使った。Slider は Min/Max/Current シナリオ を実装している。規定値はMin=0/Max=10 である。
依存関係プロパティのコールバックと検証

1)依存関係プロパティーの値を変更 ボタンをクリックすれば slider1.Value =  - 10 となる
System.Windows.Data Warning: 92 : BindingExpression (hash=14347911): Got PropertyChanged event from Slider (hash=23264094) for Value
System.Windows.Data Warning: 97 : BindingExpression (hash=14347911): GetValue at level 0 from Slider (hash=23264094) using DependencyProperty(Value): '0'
System.Windows.Data Warning: 76 : BindingExpression (hash=14347911): TransferValue - got raw value '0'
System.Windows.Data Warning: 80 : BindingExpression (hash=14347911): TransferValue - implicit converter produced '0'
System.Windows.Data Warning: 85 : BindingExpression (hash=14347911): TransferValue - using final value '0'
System.Windows.Data Warning: 92 : BindingExpression (hash=49584532): Got PropertyChanged event from Slider (hash=23264094) for Value
System.Windows.Data Warning: 97 : BindingExpression (hash=49584532): GetValue at level 0 from Slider (hash=23264094) using DependencyProperty(Value): '0'
System.Windows.Data Warning: 76 : BindingExpression (hash=49584532): TransferValue - got raw value '0'
System.Windows.Data Warning: 85 : BindingExpression (hash=49584532): TransferValue - using final value '0'

ちゃんと Binding はMin=0の強制値で伝搬する。

2)プロパティーの値を変える TextBox の値を 20 に変更すれば Mode=TwoWay で Slider.Value を変更する
System.Windows.Data Warning: 91 : BindingExpression (hash=52697953): Got LostFocus event from TextBox (hash=22597652)
System.Windows.Data Warning: 86 : BindingExpression (hash=52697953): Update - got raw value '20'
System.Windows.Data Warning: 89 : BindingExpression (hash=52697953): Update - implicit converter produced '20'
System.Windows.Data Warning: 90 : BindingExpression (hash=52697953): Update - using final value '20'
System.Windows.Data Warning: 98 : BindingExpression (hash=52697953): SetValue at level 0 to Slider (hash=59109011) using DependencyProperty(Value): '20'
System.Windows.Data Warning: 92 : BindingExpression (hash=52697953): Got PropertyChanged event from Slider (hash=59109011) for Value
System.Windows.Data Warning: 97 : BindingExpression (hash=52697953): GetValue at level 0 from Slider (hash=59109011) using DependencyProperty(Value): '10'
System.Windows.Data Warning: 76 : BindingExpression (hash=52697953): TransferValue - got raw value '10'
System.Windows.Data Warning: 80 : BindingExpression (hash=52697953): TransferValue - implicit converter produced '10'
System.Windows.Data Warning: 85 : BindingExpression (hash=52697953): TransferValue - using final value '10'
System.Windows.Data Warning: 92 : BindingExpression (hash=40644060): Got PropertyChanged event from Slider (hash=59109011) for Value
System.Windows.Data Warning: 97 : BindingExpression (hash=40644060): GetValue at level 0 from Slider (hash=59109011) using DependencyProperty(Value): '10'
System.Windows.Data Warning: 76 : BindingExpression (hash=40644060): TransferValue - got raw value '10'
System.Windows.Data Warning: 85 : BindingExpression (hash=40644060): TransferValue - using final value '10'

TextBox の値を 20 で Slider.Value には伝わるが Got PropertyChanged event from Slider で Binding はMax=10の強制値で伝搬する。

posted @ 12:28 | Feedback (13)

Mass Effect 始めました

Mass Effect
http://www.xbox.com/ja-JP/games/splash/m/masseffect/

RPGと書いてあったのでやっていますが、戦闘はRPGというよりガンシューティングです、展開や会話はアドベンチャーゲームっぽいです、今はダンジョンRPGっぽいので、いろんな要素が入っているんだと思います、キャラクタがソルジャーじゃなければ別の戦い方になるんでしょう。
RPGの次世代を告げるってのはそういう意味なのかもしれないです。

ガンシューティングが大変で最初の時間制限があるミッションで何度もやり直しました、かなりつらい状態です。
設定を初心者用にしてようやく何度目かでコツとルートがつかめてきてなんとか乗り越えました。
車に乗ってのガンシューティングも大変でした、何度も戦闘エリア外に逃げ出しながら回復してやっと越えられました。

その後はあまりガンシューティングで苦労はしていないので、結構なれとコツをつかめばうまくいくのかもしれません。
というかまだ始めたばっかりの状態に近いからかもしれません。

武器は高いです、とてもじゃないけど最初のうちは買えません、しかし拾えたりして買う必要もない気もします。
設定の文章もまとまっていてそれを読んでいるだけでも楽しかったりします。

今何をやるべきかもまとまっているしマップもあるのであまり迷わないで済みそうです。

posted @ 1:04 | Feedback (1)