C# 组合框项更改的WPF数据触发器
我用XAML设计了一个接触盒C# 组合框项更改的WPF数据触发器,c#,wpf,xaml,datatrigger,multidatatrigger,C#,Wpf,Xaml,Datatrigger,Multidatatrigger,我用XAML设计了一个接触盒 <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <ComboBox Grid.Col
<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>
类型的初始值为零(即类型=0;
)
实施的条件:
如果Type等于1或2,则需要启用文本框-IsEnabled=True
如果Type为1,则文本框.MaxLength
应为10
如果Type为2,则文本框.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>
但是上面的代码不起作用。请帮助我如何在DataTemplate
中实现DataTrigger
中的逻辑。您的文本框可能具有DataTriggers样式:
<TextBox Text="{Binding Contact, UpdateSourceTrigger=PropertyChanged}">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding Type}" Value="0">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
<DataTrigger Binding="{Binding Type}" Value="1">
<Setter Property="MaxLength" Value="10" />
</DataTrigger>
<DataTrigger Binding="{Binding Type}" Value="2">
<Setter Property="MaxLength" Value="11" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
如果在实例化DataTemplate后支持类型
属性更改其值,则所属类需要实现INotifyPropertyChanged
接口
<TextBox Text="{Binding Contact, UpdateSourceTrigger=PropertyChanged}">
<TextBox.Style>
<Style TargetType="TextBox">
<Style.Triggers>
<DataTrigger Binding="{Binding Type}" Value="0">
<Setter Property="IsEnabled" Value="False" />
</DataTrigger>
<DataTrigger Binding="{Binding Type}" Value="1">
<Setter Property="MaxLength" Value="10" />
</DataTrigger>
<DataTrigger Binding="{Binding Type}" Value="2">
<Setter Property="MaxLength" Value="11" />
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>