2016-08-03 3 views
0

CheckBox.IsCheckedのバインディングが変更されていないDataTriggerソース値によるCheckBox.IsCheckedを変更した後。CheckBoxバインド元が更新されていません

私は簡単なのViewModel

public class ViewModel : INotifyPropertyChanged 
    {  
     private bool check1;  
     public bool Check1 
     { 
      get { return check1; } 
      set { check1 = value; NotifyPropertyChanged(); } 
     }  

     private bool check2;  
     public bool Check2 
     { 
      get { return check2; } 
      set { check2 = value; NotifyPropertyChanged(); } 
     } 

     #region Notify 
     ...  
     #endregion 
    } 

と簡単なXAML

<StackPanel Grid.Row="1"> 
     <CheckBox Content="Check1"> 
      <CheckBox.Style > 
       <Style TargetType="{x:Type CheckBox}"> 
        <Setter Property="IsChecked" Value="{Binding Check1, Mode=TwoWay}"/> 
        <Style.Triggers> 
         <DataTrigger Binding="{Binding Check2}" Value="True"> 
          <Setter Property="IsChecked" Value="True"/> 
         </DataTrigger> 
        </Style.Triggers> 
       </Style> 
      </CheckBox.Style> 
     </CheckBox> 

     <CheckBox Content="Check2" IsChecked="{Binding Check2}"/> 

     <TextBlock Text="{Binding Check1}" Name="uiCheckValue1"/> 
     <TextBlock Text="{Binding Check2}" Name="uiCheckValue2"/> 
    </StackPanel> 

enter image description here 私はCheckBox2をチェックするとCheckBox1ををチェックしてしまうが、ソースが更新されていないがあります。どのようにソースを更新するには?

enter image description here

+0

"CHECK1、モード=双方向]を結合{" しようとする結合を変更します –

答えて

1

それはなぜ言うのは簡単だ:手動TrueIsCheckedを設定した場合には、バインディングを失います。あなたのコードが間違っているように思わ

public bool Check2{ 
    get { return check2; } 
    set { 
      check2 = value; 
      if (value == true) Check1 = true; 
      NotifyPropertyChanged(); 
     } 
} 
2


DataTriggerを削除し、このようなあなたのViewModelを変更します。ビューを更新するには、PropertyChangedイベントを適切に発行する必要があります。コード下 チェック:

public class ViewModel : INotifyPropertyChanged 
{  
    private bool check1;  
    public bool Check1 
    { 
     get { return check1; } 
     set 
     { 
      check1 = value; 
      PropertyChanged(value, new PropertyChangedEventArgs("Check1")); 
     } 
    }  

    private bool check2;  
    public bool Check2 
    { 
     get { return check2; } 
     set 
     { 
      check2 = value; 
      PropertyChanged(value, new PropertyChangedEventArgs("Check1")); 
     } 
    } 

    #region Notify 
    ...  
    #endregion 
} 

またTwoWay

<<CheckBox Content="Check2" IsChecked="{Binding Check2, Mode=TwoWay}"/> 

<DataTrigger Binding="{Binding Check2, Mode=TwoWay}" Value="True"> 
<Setter Property="IsChecked" Value="True" /> 
</DataTrigger> 
関連する問題