2017-03-04 6 views
0

私は、DataTemplateトリガーで非uiプロパティの値を更新できるかどうかを調べようとしています。それは動作していないようです。下記参照。 Personオブジェクトを表示するためにクリックすると、チェックボックスがオフの場合、インスタンスはValidプロパティtrueを表示します。DataTemplate.Triiggers非UI要素へのバインド

のPersonオブジェクト:

namespace WPFControls 
{ 
    public class Person : INotifyPropertyChanged 
    { 
     public string Name { get; set; } 

     private double _age = 0; 
     public double Age 
     { 
      get { return _age; } 
      set 
      { 
       if (_age != value) 
       { 
        _age = value; 
        OnPropertyChanged(nameof(Age)); 
       } 
       else 
       { 
        Debug.WriteLine("NO CHANGE"); 
       } 

      } 
     } 

     private bool valid = true; 
     public bool IsValid { 
      get { return valid; } 
      set 
      { 
       if (valid != value) 
       { 
        valid = value; 
        OnPropertyChanged(nameof(IsValid)); 
       } 
       else 
       { 
        Debug.WriteLine("isvalud no change"); 
       } 
      } 
     } 

     public override string ToString() 
     { 
      return $"{nameof(Name)}: {Name}, {nameof(Age)}: {Age} valid: {nameof(IsValid)} {IsValid}"; 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 

     [NotifyPropertyChangedInvocator] 
     protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
     { 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
} 

XAML:

<Window x:Class="WPFControls.AgeForm" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:WPFControls" 
     mc:Ignorable="d" 
     Title="AgeForm" Height="300" Width="350"> 

    <Window.Resources> 
     <DataTemplate DataType="{x:Type local:Person}"> 
      <Grid Margin="10,10,10,10"> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="40"></RowDefinition> 
        <RowDefinition Height="40"></RowDefinition> 
        <RowDefinition Height="40"></RowDefinition> 
       </Grid.RowDefinitions> 

       <Grid.ColumnDefinitions> 
        <ColumnDefinition></ColumnDefinition> 
        <ColumnDefinition></ColumnDefinition> 
       </Grid.ColumnDefinitions> 

       <TextBlock Grid.Row="0" Grid.Column="0">Name:</TextBlock> 
       <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" Margin="8"/> 

       <TextBlock Name="AgeText" Grid.Row="1" Grid.Column="0">Age:</TextBlock> 
       <TextBox Name="AgeBox" Grid.Row="1" Grid.Column="1" Text="{Binding Age, UpdateSourceTrigger=PropertyChanged}" Margin="8"/> 

       <CheckBox Margin="50,0,0,0" Grid.Row="0" Grid.Column="0" Name="CheckBoxValid" IsChecked="{Binding IsValid, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Content="IS VALID?"/> 
      </Grid> 

      <DataTemplate.Triggers> 
       <DataTrigger Binding="{Binding Age}" Value="21"> 
        <Setter TargetName="AgeText" Property="Background" Value="Red"/> 
        <Setter TargetName="CheckBoxValid" Property="CheckBox.IsChecked" Value="False"/> 
       </DataTrigger> 
      </DataTemplate.Triggers> 
     </DataTemplate> 

    </Window.Resources> 


    <Grid Margin="20,20,20,20"> 

     <Grid.RowDefinitions> 
      <RowDefinition Height="200"/> 
      <RowDefinition/> 
     </Grid.RowDefinitions> 
     <ContentControl Content="{Binding}"/> 

     <StackPanel Grid.Row="1"> 
      <Button Name="ShowButton" Click="ShowButton_OnClick">_Show...</Button> 
     </StackPanel> 

    </Grid> 
</Window> 

注クリックイベントだけMessageBox.Show(persion.ToString())

答えて

1

セッター<Setter TargetName="CheckBoxValid" Property="CheckBox.IsChecked" Value="False"/>は既存のバインディングを削除し、Personプロパティは更新されません。ですから、私はそのセッターを削除し、ビューモデルで検証ロジックを追加することをお勧めします:

public double Age 
{ 
    get { return _age; } 
    set 
    { 
     if (_age != value) 
     { 
      _age = value; 
      OnPropertyChanged(nameof(Age)); 
      if (_age == 21) IsValid = false; 
     } 
     else 
     { 
      Debug.WriteLine("NO CHANGE"); 
     } 

    } 
} 
関連する問題