4

初期値がtrueに設定された後、トグルボタンへの双方向バインディングが切り離されます。ボタンのトグルを外すと、バインドされなくなります。

2つのトグルボタンがあります。

<RadioButton x:Name="BackupButton" Style="{StaticResource {x:Type ToggleButton}}" DataContext="{Binding BackupVM}" IsChecked="{Binding Mode=TwoWay, Path=IsViewVisible}">Backup</RadioButton>
<RadioButton x:Name="RestoreButton" Style="{StaticResource {x:Type ToggleButton}}">Restore</RadioButton>

バインドしたいBackupViewModel(BackupVMとしてインスタンス化)のプロパティ:

private bool _IsViewVisible = true;
public bool IsViewVisible
{
        get { return _IsViewVisible; }
        set
        {
            if (value != _IsViewVisible)
            {
                _IsViewVisible = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("IsViewVisible"));
            }
        }
    }

一方をオンに切り替えると、特定のユーザーコントロール(表示)を表示し、もう一方を非表示にします。私がする必要があるのは、ビューが非表示になっていることを基になるビューモデルに伝えて、一部のデータを更新するタイマーを停止できるようにすることです。ロード時にIsChecked値が設定された後、何らかの理由でバインディングが切り離されます。トレースを実行した後の出力は次のとおりです。

System.Windows.Data Warning: 52 : Created BindingExpression (hash=9343812) for Binding (hash=58368655)
System.Windows.Data Warning: 54 :   Path: 'IsViewVisible'
System.Windows.Data Warning: 57 : BindingExpression (hash=9343812): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 58 : BindingExpression (hash=9343812): Attach to         System.Windows.Controls.RadioButton.IsChecked (hash=17818390)
System.Windows.Data Warning: 63 : BindingExpression (hash=9343812): Resolving source 
System.Windows.Data Warning: 66 : BindingExpression (hash=9343812): Found data context element: RadioButton (hash=17818390) (OK)
System.Windows.Data Warning: 74 : BindingExpression (hash=9343812): Activate with root item <null>
System.Windows.Data Warning: 102 : BindingExpression (hash=9343812):   Item at level 0 is null - no accessor
System.Windows.Data Warning: 76 : BindingExpression (hash=9343812): TransferValue - got raw value {DependencyProperty.UnsetValue}
System.Windows.Data Warning: 84 : BindingExpression (hash=9343812): TransferValue - using fallback/default value 'False'
System.Windows.Data Warning: 85 : BindingExpression (hash=9343812): TransferValue - using final value 'False'
System.Windows.Data Warning: 92 : BindingExpression (hash=9343812): Got PropertyChanged event from RadioButton (hash=17818390) for DataContext
System.Windows.Data Warning: 75 : BindingExpression (hash=9343812): Deactivate
System.Windows.Data Warning: 99 : BindingExpression (hash=9343812): Replace item at level 0 with {NullDataItem}
System.Windows.Data Warning: 74 : BindingExpression (hash=9343812): Activate with root item BackupViewModel (hash=58266349)
System.Windows.Data Warning: 104 : BindingExpression (hash=9343812):   At level 0 - for BackupViewModel.IsViewVisible found accessor RuntimePropertyInfo(IsViewVisible)
System.Windows.Data Warning: 100 : BindingExpression (hash=9343812): Replace item at level 0 with BackupViewModel (hash=58266349), using accessor RuntimePropertyInfo(IsViewVisible)
System.Windows.Data Warning: 97 : BindingExpression (hash=9343812): GetValue at level 0 from BackupViewModel (hash=58266349) using RuntimePropertyInfo(IsViewVisible): 'True'
System.Windows.Data Warning: 76 : BindingExpression (hash=9343812): TransferValue - got raw value 'True'
System.Windows.Data Warning: 85 : BindingExpression (hash=9343812): TransferValue - using final value 'True'
System.Windows.Data Warning: 75 : BindingExpression (hash=9343812): Deactivate
System.Windows.Data Warning: 99 : BindingExpression (hash=9343812): Replace item at level 0 with {NullDataItem}
System.Windows.Data Warning: 59 : BindingExpression (hash=9343812): Detach
4

2 に答える 2

1

本当に「バインド」されていませんか?私にもそれが起こったことがありますが、あなたにはそれが欠けているように見えます...あなたは何も示していません

NotifyOnSourceUpdated=true

{binding Mode....} コンテンツで

于 2013-02-15T02:20:40.357 に答える
0

それは構文のようです...それはあなたViewModelにまったく拘束されていないようです。これを試して:

<RadioButton Grid.Row="0" x:Name="BackupButton" Style="{StaticResource {x:Type ToggleButton}}" IsChecked="{Binding IsViewVisible, Mode=TwoWay}">
    Backup
    <RadioButton.DataContext>
        <local:BackupVM />
    </RadioButton.DataContext>
</RadioButton>

を次のように定義する場所local namespace:

xmlns:local="clr-namespace:WpfApplication1"

注:WpfApplication1を名前空間の名前に置き換えます。

于 2013-02-15T03:22:23.250 に答える