我是WPF的新手(和一般的.Net)。我有2个ComboBox,希望用第一个控制第二个的可见性:
如果被选中" Uno"在左侧ComboBox中,右侧ComboBox被隐藏。
这是我的XAML:
<Window x:Class="WpfApp1.MainWindow"
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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox x:Name="Left" HorizontalAlignment="Left" Margin="66,47,0,0" VerticalAlignment="Top" Width="120">
<ComboBoxItem Content="Uno"></ComboBoxItem>
<ComboBoxItem Content="Dos"></ComboBoxItem>
</ComboBox>
<ComboBox x:Name="Right" HorizontalAlignment="Left" Margin="326,47,0,0" VerticalAlignment="Top" Width="120">
<Style TargetType="ComboBox">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=Left, Path=SelectedItem}" Value="Uno">
<Setter Property="Visibility" Value="Hidden"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox>
</Grid>
我也试过
<DataTrigger Binding="{Binding ElementName=Left, Path=SelectedIndex}" Value="0">
<Setter Property="Visibility" Value="Hidden"></Setter>
</DataTrigger>
感谢您的帮助和时间
答案 0 :(得分:4)
只需设置右Style
的{{1}}属性并绑定到ComboBox
:
SelectedItem.Content
答案 1 :(得分:1)
<Grid>
<Grid.Resources>
<local:IntToVisibilityConverter x:Key="IntToVisibilityConverter" />
</Grid.Resources>
<ComboBox x:Name="Left" HorizontalAlignment="Left" Margin="66,47,0,0" VerticalAlignment="Top" Width="120">
<ComboBoxItem Content="Uno"></ComboBoxItem>
<ComboBoxItem Content="Dos"></ComboBoxItem>
</ComboBox>
<ComboBox x:Name="Right" HorizontalAlignment="Left" Margin="326,47,0,0" VerticalAlignment="Top" Width="120" Visibility="{Binding ElementName=Left, Path=SelectedIndex,Converter={StaticResource IntToVisibilityConverter}}">
</ComboBox>
</Grid>
public class IntToVisibilityConverter: IValueConverter
{
/// <summary>
/// Used to Convert a value
/// </summary>
public object Convert(object value, Type targetType, object parameter, string language)
{
int index= (int)value;
return index==0? Visibility.Hidden : Visibility.Visible;
}
/// <summary>
/// Inverse of Convert method
/// </summary>
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}