在按钮悬停时,启用ResizeMode

时间:2013-09-18 20:00:20

标签: wpf

我试图在用户悬停“mybutton”时显示窗口框。这应该有效,但由于某种原因,它不是。我错过了什么?

<Window x:Class="test2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" WindowStyle="None"  Loaded="ShellWindow_SourceInitialized"   x:Name="mywindow">
<Window.Resources>
    <Style TargetType="{x:Type Window}">            
        <Style.Triggers>            
            <DataTrigger Binding="{Binding Path=IsMouseOver, ElementName=mybutton}" Value="True">
                <Setter Property="ResizeMode" Value="CanResize" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Path=IsMouseOver, ElementName=mybutton}" Value="False">
                <Setter Property="ResizeMode" Value="NoResize" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

</Window.Resources>
<Grid Name="mygrid" Loaded="Grid_Loaded">
    <Button Name="mybutton" Content="Button" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="75"/>
</Grid>

(如果您想知道这些“已加载”功能是什么,它们会在保留阴影的同时删除chrome并允许transnsparency = false http://marcin.floryan.pl/blog/2010/08/wpf-drop-shadow-with-windows-dwm-api) 最后它不会是按钮,但会对整个窗口边框作出反应(我找不到更好的方法来启用调整大小同时删除所有的铬)

2 个答案:

答案 0 :(得分:2)

样式TargetType应该是MainWindow

&LT; Style TargetType="{x:Type local:MainWindow}">
必须将local映射到您的命名空间:

xmlns:local="clr-namespace:test2"

答案 1 :(得分:1)

myButton是在样式之后定义的。 XAML解析器并不那么聪明。使用以下XAML来获得您的预期行为:

<Window x:Class="test2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" WindowStyle="None"  Loaded="ShellWindow_SourceInitialized"   x:Name="mywindow">

    <Grid Name="mygrid" Loaded="Grid_Loaded" >
        <Button Name="mybutton" Content="Button" HorizontalAlignment="Left"  VerticalAlignment="Top" Width="75"/>
    </Grid>
    <Window.Style>
        <Style TargetType="{x:Type Window}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=IsMouseOver, ElementName=mybutton}" Value="True">
                    <Setter Property="ResizeMode" Value="CanResize" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Path=IsMouseOver, ElementName=mybutton}" Value="False">
                    <Setter Property="ResizeMode" Value="NoResize" />
                </DataTrigger>
            </Style.Triggers>
        </Style>

    </Window.Style>
</Window>