我希望每次悬停一个togglebutton时都会出现一个弹出窗口。然后它需要保持打开状态,直到我点击应用程序中的其他位置。下面的代码在启动时工作正常,但只要我选中或取消选中togglebutton,弹出窗口就会拒绝显示。关于我做错了什么想法?
WPF代码
<ToggleButton Name="btnLogFile" Style="{StaticResource StandardToggle}"
Grid.Row="1" Grid.Column="3" Margin="0,3,3,0"
MouseEnter="btnLogFile_MouseEnter">
<Path Margin="7" SnapsToDevicePixels="True" Stretch="Uniform"
Stroke="{StaticResource TextLight}" StrokeThickness="2">
<Path.Data>
<GeometryGroup FillRule="Nonzero">
<PathGeometry Figures="M 0 0 L 20 0 L 20 10 L 30 10 L 30 40 L 0 40 Z" />
<PathGeometry Figures="M 20 0 L 22 0 L 30 8 L 30 10" />
</GeometryGroup>
</Path.Data>
</Path>
</ToggleButton>
<Popup Name="popLogFile"
PlacementTarget="{Binding ElementName=btnLogFile}" Placement="Custom"
HorizontalOffset="0" VerticalOffset="0"
MouseLeftButtonDown="popLogFile_MouseLeftButtonDown">
<Border Background="{StaticResource BackgroundDark}" BorderBrush="{StaticResource TextBoxBorder}" BorderThickness="1"
Width="300" Height="Auto">
<Grid Margin="3">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="3" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="3" />
<ColumnDefinition Width="22" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="22" />
</Grid.RowDefinitions>
<TextBlock Margin="0,1" Grid.Row="0" Grid.Column="0" Foreground="{StaticResource TextLight}" HorizontalAlignment="Right">Directory</TextBlock>
<TextBox Name="logfilePath" Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="3"
Style="{StaticResource StandardTextBox}"
Foreground="{StaticResource TextLight}">
C:\logfile.txt
</TextBox><!-- Button made invisible for the time being -->
<Button Name="btnBrowseLogfile" Style="{StaticResource StandardButton}" Grid.Row="0" Grid.Column="4" Visibility="Collapsed">...</Button>
</Grid>
</Border>
</Popup>
togglebutton的鼠标事件:
private void btnLogFile_MouseEnter(object sender, MouseEventArgs e)
{
this.popLogFile.IsOpen = true;
this.popLogFile.StaysOpen = false;
}
答案 0 :(得分:3)
不要使用代码来操纵UIElements。将Popup的IsOpen
属性绑定到togglebutton的IsMouseOver
属性。或者,如果您需要多个或更多复杂条件,请将Style
设置为弹出窗口,并且样式可以包括Triggers
或DataTriggers
。我建议你看一下这个WPF Tutorial
编辑:
应该是这样的:
<DataTrigger Binding="{Binding IsChecked, ElementName=YourToggleButton}" Value="True">
<Setter TargetName="ThePopup" Property="IsOpen" Value="True"/>
</DataTrigger>
答案 1 :(得分:2)
1)向ToggleButton添加事件触发器:
<ToggleButton Name="btnLogFile"
Style="{StaticResource StandardToggle}"
Grid.Row="1" Grid.Column="3" Margin="0,3,3,0" >
<ToggleButton.Triggers>
<EventTrigger RoutedEvent="MouseEnter">
<BeginStoryboard>
<Storyboard TargetName="popLogFile" TargetProperty="IsOpen">
<BooleanAnimationUsingKeyFrames FillBehavior="HoldEnd">
<DiscreteBooleanKeyFrame
KeyTime="00:00:00"
Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="ToggleButton.Checked">
<BeginStoryboard>
<Storyboard TargetName="popLogFile" TargetProperty="IsOpen">
<BooleanAnimationUsingKeyFrames FillBehavior="HoldEnd">
<DiscreteBooleanKeyFrame
KeyTime="00:00:00"
Value="False" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="ToggleButton.Unchecked">
<BeginStoryboard>
<Storyboard TargetName="popLogFile" TargetProperty="IsOpen">
<BooleanAnimationUsingKeyFrames FillBehavior="HoldEnd">
<DiscreteBooleanKeyFrame
KeyTime="00:00:00"
Value="False" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</ToggleButton.Triggers>
<Path Margin="7" SnapsToDevicePixels="True" Stretch="Uniform"
...
2)在XAML中设置StaysOpen =“False”:
<Popup Name="popLogFile"
PlacementTarget="{Binding ElementName=btnLogFile}" Placement="Custom"
HorizontalOffset="0" VerticalOffset="0"
StaysOpen="False"
MouseLeftButtonDown="popLogFile_MouseLeftButtonDown">
3)删除方法btnLogFile_MouseEnter
答案 2 :(得分:1)
您可以将Popup.IsOpen和ToggleButton.IsChecked绑定到相同的bool属性。请在此处查看演示代码http://bit.ly/L5jxFl