XAML で連絡先ボックスを設計しています
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ComboBox Grid.Column="0" SelectedItem="{Binding Type, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
<ComboBoxItem Content="1">Mobile</ComboBoxItem>
<ComboBoxItem Content="2">Phone</ComboBoxItem>
</ComboBox>
<TextBox Text="{Binding Contact, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" />
</Grid>
</DataTemplate>
プロパティは
public int Type { get; set; }
public string Contact { get; set; }
Type is ZERO
(つまり、 )の初期値Type = 0;
。
実施条件:
- Type が 1 または 2 の場合、TextBox を有効にする必要があります -
IsEnabled = True
- Type が 1 の場合、
TextBox.MaxLength
は 10 である必要があります - Type が 2 の場合、
TextBox.MaxLength
は 11 になります。
次のコードを試しました:
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Type}" Value="0">
<Setter Property="TextBox.IsEnabled" Value="False" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=Type}" Value="1">
<Setter Property="TextBox.MaxLength" Value="10" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=Type}" Value="2">
<Setter Property="TextBox.MaxLength" Value="11" />
</DataTrigger>
</DataTemplate.Triggers>
しかし、上記のコードは機能しません。DataTrigger
内でロジックを実現する方法を教えてくださいDataTemplate
。