cra*_*mes 20 c# wpf xaml popup togglebutton
我试图从用户界面级别做一些看似相对简单和逻辑的东西,但我有一个非常烦人的错误.我有一个ToggleButton,我试图显示一个Popup按钮切换时,并隐藏Popup按钮切换时.在Popup当用户点击远离它也隐藏了.
一切都按照预期的方式使用以下XAML,除非在显示后单击切换按钮Popup,Popup消失一瞬间然后重新出现.
我怀疑这里发生了什么,点击远离Popup它是导致它关闭按钮然后立即切换按钮,因为鼠标点击它.我只是不知道如何修复它.
任何帮助表示赞赏.谢谢.
<ToggleButton x:Name="TogglePopupButton" Content="My Popup Toggle Button" Width="100" />
<Popup StaysOpen="False" IsOpen="{Binding IsChecked, ElementName=TogglePopupButton, Mode=TwoWay}">
<Border Width="100" Height="200" Background="White" BorderThickness="1" BorderBrush="Black">
<TextBlock>This is a test</TextBlock>
</Border>
</Popup>
Run Code Online (Sandbox Code Playgroud)
sch*_*ola 35
Stephans的答案有一个缺点,即每当它失去焦点时关闭弹出窗口的所需行为也会消失.
我通过在弹出窗口打开时禁用切换按钮解决了这个问题.另一种方法是使用IsHitTestVisible属性而不是启用:
<ToggleButton x:Name="TogglePopupButton" Content="My Popup Toggle Button" Width="100" IsEnabled="{Binding ElementName=ToggledPopup, Path=IsOpen, Converter={StaticResource BoolToInvertedBoolConverter}}"/>
<Popup x:Name="ToggledPopup" StaysOpen="False" IsOpen="{Binding IsChecked, ElementName=TogglePopupButton, Mode=TwoWay}">
<Border Width="100" Height="200" Background="White" BorderThickness="1" BorderBrush="Black">
<TextBlock>This is a test</TextBlock>
</Border>
</Popup>
Run Code Online (Sandbox Code Playgroud)
转换器看起来像这样:
public class BoolToInvertedBoolConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is bool)
{
bool boolValue = (bool)value;
return !boolValue;
}
else
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException("ConvertBack() of BoolToInvertedBoolConverter is not implemented");
}
}
Run Code Online (Sandbox Code Playgroud)
小智 21
没有IValueConverter的解决方案:
<Grid>
<ToggleButton x:Name="TogglePopupButton" Content="My Popup Toggle Button" Width="100" >
<ToggleButton.Style>
<Style TargetType="{x:Type ToggleButton}">
<Setter Property="IsHitTestVisible" Value="True"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=Popup, Path=IsOpen}" Value="True">
<Setter Property="IsHitTestVisible" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</ToggleButton.Style>
</ToggleButton>
<Popup StaysOpen="false" IsOpen="{Binding IsChecked, ElementName=TogglePopupButton, Mode=TwoWay}"
PlacementTarget="{Binding ElementName=TogglePopupButton}" PopupAnimation="Slide"
x:Name="Popup">
<Border Width="100" Height="200" Background="White" BorderThickness="1" BorderBrush="Black">
<TextBlock>This is a test</TextBlock>
</Border>
</Popup>
</Grid>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11518 次 |
| 最近记录: |