我在XAML中有四张图片:
<Image Visibility="Collapsed" Name="LeftArrow" Height="20" Width="20"></Image>
<Image Visibility="Collapsed" Name="RightArrow" Height="20" Width="20"></Image>
<Image Visibility="Collapsed" Name="TopArrow" Height="20" Width="20"></Image>
<Image Visibility="Collapsed" Name="BottomArrow" Height="20" Width="20"></Image>
在我的代码隐藏中,我有一个切换案例,当其中一个图像可见时,所有其他图像都会崩溃:
switch (ElementInfoCollection[pos].ArrowDirection)
{
case ArrowDirection.Left:
LeftArrow.Visibility = Visibility.Visible;
RightArrow.Visibility = Visibility.Collapsed;
TopArrow.Visibility = Visibility.Collapsed;
BottomArrow.Visibility = Visibility.Collapsed;
break;
case ArrowDirection.Right:
LeftArrow.Visibility = Visibility.Collapsed;
RightArrow.Visibility = Visibility.Visible;
TopArrow.Visibility = Visibility.Collapsed;
BottomArrow.Visibility = Visibility.Collapsed;
break;
case ArrowDirection.Top:
LeftArrow.Visibility = Visibility.Collapsed;
RightArrow.Visibility = Visibility.Collapsed;
TopArrow.Visibility = Visibility.Visible;
BottomArrow.Visibility = Visibility.Collapsed;
break;
case ArrowDirection.Bottom:
LeftArrow.Visibility = Visibility.Collapsed;
RightArrow.Visibility = Visibility.Collapsed;
TopArrow.Visibility = Visibility.Collapsed;
BottomArrow.Visibility = Visibility.Visible;
break;
default:
LeftArrow.Visibility = Visibility.Collapsed;
RightArrow.Visibility = Visibility.Collapsed;
TopArrow.Visibility = Visibility.Collapsed;
BottomArrow.Visibility = Visibility.Collapsed;
break;
}
显然,这是一种非常愚蠢的方式,所以我想知道如果在代码隐藏中将其中一个设置为可见时我可以使图像崩溃?这就是我到目前为止所提出的:
<UserControl.Resources>
<Style x:Key="ArrowSwitch" TargetType="{x:Type Image}">
<Style.Triggers>
<DataTrigger >
<Setter Property="Image.Visibility" Value="Collapsed"></Setter>
</DataTrigger>
<DataTrigger>
<Setter Property="Image.Visibility" Value="Visible"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
答案 0 :(得分:3)
这是相同的代码,但更短:
switch (ElementInfoCollection[pos].ArrowDirection)
{
LeftArrow.Visibility = Visibility.Collapsed;
RightArrow.Visibility = Visibility.Collapsed;
TopArrow.Visibility = Visibility.Collapsed;
BottomArrow.Visibility = Visibility.Collapsed;
case ArrowDirection.Left: LeftArrow.Visibility = Visibility.Visible; break;
case ArrowDirection.Right: RightArrow.Visibility = Visibility.Visible; break;
case ArrowDirection.Top: TopArrow.Visibility = Visibility.Visible; break;
case ArrowDirection.Bottom: BottomArrow.Visibility = Visibility.Visible; break;
default: break;
}
答案 1 :(得分:1)
如果要使用数据绑定,可以使用将ArrowDirection映射到Visibility值的转换器:
public class DirectionVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
ArrowDirection target;
ArrowDirection current;
try
{
current = (ArrowDirection)value;
}
catch (Exception)
{
return null;
}
if (Enum.TryParse(parameter.ToString(), out target))
{
if (current == target)
return Visibility.Visible;
else
return Visibility.Collapsed;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
并在XAML中使用它,如:
<Window.Resources>
<local:DirectionVisibilityConverter x:Key="VisibilityConverter"/>
</Window.Resources>
....
<Image Visibility="{Binding ArrowDirection, Converter={StaticResource VisibilityConverter}, ConverterParameter=Left}" .../>
其中ArrowDirection
指定ElementInfoCollection[pos].ArrowDirection
的绑定路径。如果它实现INotifyPropertyChanged
,您只需将此值设置为正确的方向,并且可见性将更新。