gpt4 book ai didi

wpf - IsMouseOver、可见性和控制尺寸

转载 作者:行者123 更新时间:2023-12-02 11:19:20 27 4
gpt4 key购买 nike

WPF:

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525">

<Window.Resources>
<local:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"
True="Visible"
False="Collapsed" />
</Window.Resources>

<Canvas>
<StackPanel Canvas.Left="100" Canvas.Top="100" Width="200" Height="100">
<ListBox>
<ListBoxItem>one</ListBoxItem>
<ListBoxItem>two</ListBoxItem>
<ListBoxItem>three</ListBoxItem>
</ListBox>
<StackPanel Orientation="Horizontal"
Visibility="{Binding
RelativeSource={RelativeSource AncestorType={x:Type StackPanel}},
Path=IsMouseOver,
Converter={StaticResource BooleanToVisibilityConverter}}">
<Button>one</Button>
<Button>two</Button>
<Button>three</Button>
</StackPanel>
</StackPanel>
</Canvas>
</Window>

代码:

public abstract class BooleanConverter<T> : IValueConverter
{
public BooleanConverter(T trueValue, T falseValue)
{
True = trueValue;
False = falseValue;
}

public T True { get; set; }
public T False { get; set; }

public virtual object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is bool && ((bool)value) ? True : False;
}

public virtual object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is T && EqualityComparer<T>.Default.Equals((T)value, True);
}
}

public class BooleanToVisibilityConverter : BooleanConverter<Visibility>
{
public BooleanToVisibilityConverter()
: base(Visibility.Visible, Visibility.Collapsed)
{ }
}

我的鼠标悬停在列表框上,按钮按预期显示。如果鼠标移动到右侧列表框下方,按钮就会消失。为什么 IsMouseOver 那里是假的?当内部 StackPanel 的 Visibility 属性从 Collapsed 更改为 Visible 时,外部 StackPanel 的高度不应该增加吗?

如果您想玩一下,这是该项目:http://dl.dropbox.com/u/4220513/WpfApplication1.zip

enter image description here

最佳答案

您需要将 StackPanel(外部)的背景设​​置为透明才能检测鼠标悬停。如果像示例中那样背景为空,则 HitTest 将失败。

关于wpf - IsMouseOver、可见性和控制尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6601727/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com