开发者

IsMouseOver, Visibility, and Control dimensions

WPF:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.mi开发者_运维问答crosoft.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>

Code:

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)
    { }
}

My the mouse hovers over the ListBox, the buttons are displayed as expected. If the mouse moves below the ListBox on the right side, the buttons vanish. Why is IsMouseOver false over there? Shouldn't the height of the outer StackPanel increase when the inner StackPanel's Visibility property changes from Collapsed to Visible?

Here's the project if you want to play with it: http://dl.dropbox.com/u/4220513/WpfApplication1.zip

IsMouseOver, Visibility, and Control dimensions


You need to set the background of the StackPanel (the outer one) to Transparent to detect mouse over. If background is null as in your sample, hit testing will fail.


Use a background for your StackPanel like <StackPanel Background="Transparent">...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜