-1

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;

実施条件:

  1. Type が 1 または 2 の場合、TextBox を有効にする必要があります -IsEnabled = True
  2. Type が 1 の場合、TextBox.MaxLengthは 10 である必要があります
  3. 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

4

1 に答える 1