2009-12-16 37 views
0

我有一個ToggleButton發生故障。據我瞭解,一個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> 

回答

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; 
} 

希望這會有所幫助,至於

+0

+1。我認爲這正是答案:)! – Anvaka 2009-12-16 06:31:59

+0

我希望這樣做不需要添加代碼。我想這是必需的 – Vaccano 2009-12-16 21:15:53

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 
    } 
} 
+0

我希望彈出窗口顯示切換btn時,並再次點擊時消失。我可以用代碼來做到這一點,但我想用綁定來完成。 – Vaccano 2009-12-16 21:16:41

+0

上面的代碼就是這麼做的。它沒有做的是按下輸入按鈕關閉彈出窗口。但一個鼠標點擊工作。 – Dabblernl 2009-12-16 21:37:37

1

當你點擊切換按鈕。它會檢查或不檢查,請記住這一點。在第一次點擊它時,它將被聚焦。 請嘗試:

<ToggleButton Focusable="False"/> 

希望能幫助你

相關問題