[英]Can I trigger an event in XAML when a control is less than a certain height?
我有一个包含两行的网格,其中填充有图像和标签。 标签具有锁定的大小,而上方的图像随网格缩小。 当图像变得太小而无法真正看到时(例如<30像素高),我想将其可见性设置为“塌陷”,我认为这会导致标签移动到网格的中心。
如果这完全是在XAML中使用某种触发器来寻找高度<x的话,那就太好了。
这可能吗? 如果没有,那么在c#中检查图像高度的最佳解决方案是什么?
这是一种方法。 首先,您需要一个转换器:
public class HeightToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var gridHeight = System.Convert.ToDouble(value);
return gridHeight < 200 ? Visibility.Collapsed : Visibility.Visible;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
这是xaml:
<Window x:Class="WpfApplication1.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:local="clr-namespace:WpfApplication1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="525"
Height="350"
mc:Ignorable="d">
<Grid Name="grid">
<Grid.Resources>
<local:HeightToVisibilityConverter x:Key="HeightToVisibilityConverter" />
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label Grid.Row="0"
Content="Some Text" />
<Image Grid.Row="1"
Source="zeros.jpg"
Visibility="{Binding ElementName=grid,
Path=ActualHeight,
Converter={StaticResource HeightToVisibilityConverter}}" />
</Grid>
</Window>
仅出于测试目的,如果栅格高度小于200,我将其设置为隐藏。您可以将其更改为适合您的情况的任何值。 我并不是说这是最佳或唯一的解决方案,但希望它能帮助您入门...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.