我有一个故障的ToggleButton。据我了解,单击时应检查ToggleButton,再次单击时应取消选中。
此示例中的ToggleButton没有。单击它只是将其设置为再次检查。任何想法为什么?
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<ToggleButton Width="100" Height="35" Name="btnAddLinkComment" >
<CheckBox Content=" Comment" FlowDirection="RightToLeft" IsHitTestVisible="False"
Focusable="False" IsChecked="{Binding ElementName=txtLinkComment, Path=Text}"
Name="chkHasComment" Margin="5"/>
</ToggleButton>
<Popup IsOpen="{Binding ElementName=btnAddLinkComment,Path=IsChecked}"
PlacementTarget="{Binding ElementName=btnAddLinkComment}" Name="popAddCommentLink"
AllowsTransparency="True" StaysOpen="False" PopupAnimation="Fade" HorizontalOffset="-50"
VerticalOffset="50">
<Border BorderBrush="#FF000000" Background="LightBlue" BorderThickness="1,1,1,1"
CornerRadius="8,8,8,8" Padding="5">
<Grid Background="LightBlue">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80"></ColumnDefinition>
<ColumnDefinition Width="200"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock TextWrapping="Wrap" Foreground="Black">Enter Link Comment:</TextBlock>
<TextBox Grid.Column="1" Name="txtLinkComment" Width="200"></TextBox>
</Grid>
</Border>
</Popup>
</Grid>
</Page>
答案 0 :(得分:1)
我猜这是因为弹出窗口被弯曲到btnAddLinkComment.isChecked属性。我相信当你显示弹出窗口时,你点击按钮会使它关闭,并将按钮的IsChecked字段设置为false,这会使按钮处于无遮挡状态;然后点击由按钮本身处理,因为它没有切换,它变为切换,弹出再次显示。我想你可以通过删除绑定来解决问题,并在代码中进行一些处理;像这样的人:
btnAddLinkComment.Click += btnAddLinkComment_Click;
popAddCommentLink.Closed += popAddCommentLink_Closed;
private void btnAddLinkComment_Click(object sender, RoutedEventArgs e)
{
if (popAddCommentLink.IsOpen && btnAddLinkComment.IsChecked == false)
popAddCommentLink.IsOpen = false;
else if (!popAddCommentLink.IsOpen && btnAddLinkComment.IsChecked == true)
popAddCommentLink.IsOpen = true;
}
private void popAddCommentLink_Closed(object sender, EventArgs e)
{
btnAddLinkComment.IsChecked = false;
}
希望这有帮助,尊重
答案 1 :(得分:1)
我不完全确定你想要完成什么,但下面的代码可能是朝着正确方向迈出的一步。请详细说明!
<Window x:Class="ToggleButtonSpike.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
xmlns:local="clr-namespace:ToggleButtonSpike">
<Window.Resources>
<local:TextToBool x:Key="StringToBool"/>
</Window.Resources>
<StackPanel>
<ToggleButton Name="Toggle" >
<CheckBox IsHitTestVisible="False"
Content="{Binding ElementName=Comment, Path=Text,
UpdateSourceTrigger=PropertyChanged}"
IsChecked="{Binding ElementName=Comment, Path=Text,
Converter={StaticResource StringToBool}}"/>
</ToggleButton>
<Popup IsOpen="{Binding ElementName=Toggle, Path=IsChecked}"
PlacementTarget="{Binding ElementName=Toggle}">
<StackPanel>
<TextBlock Foreground="White">
Enter comment:
</TextBlock>
<TextBox Name="Comment"/>
</StackPanel>
</Popup>
</StackPanel>
</Window>
using System;
using System.Windows;
using System.Windows.Data;
namespace ToggleButtonSpike
{
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
}
public class TextToBool : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return !string.IsNullOrEmpty((string)value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
}
答案 2 :(得分:1)
单击切换按钮时。它会检查或取消选中,请记住。在第一次点击它时,它将被聚焦。 请尝试:
<ToggleButton Focusable="False"/>
希望能帮到你