2013-04-24 32 views
0

我使用CheckBox,Lable進行綁定,實現了BoolToVisibleOrHidden類,意思是當checkBox1.IsChecked應該顯示Lable,我想實現的是在checkbox中檢查EventHandler,我想用MessageBox來實現。如果Messabox.Yes那麼只應該顯示標籤,WPF,C#使用VS2010

<CheckBox Name="_checkBoxExpertMode" IsChecked="{Binding Path=DisplayChecked, Mode=TwoWay}" 
<Lable Name="_lableDisplay" Visibility="{Binding Path=DisplayChecked, Mode=OneWay, NotifyOnTargetUpdated=True, Converter={StaticResource BoolToVisConverter}}" 
                  /> 

System.Windows.Forms.DialogResult dialogResult = System.Windows.Forms.MessageBox.Show("Sure", "Some Title", System.Windows.Forms.MessageBoxButtons.YesNo); 
       if (dialogResult == System.Windows.Forms.DialogResult.Yes) 
      { 
       _checkBoxExpertMode.IsChecked = true; 
      } 
      else if (dialogResult == System.Windows.Forms.DialogResult.No) 
      { 
       _checkBoxExpertMode.IsChecked = false; 
      } 

但是標籤顯示在消息框彈出之前。

幫助我,在此先感謝

+4

可以用更好的格式編寫代碼嗎? – Maverick 2013-04-24 09:39:49

回答

2

降複選框上的綁定並使用Checkbox.Checked event

<CheckBox Name="..." Checked="CheckBox_Checked" /> 

在事件處理中,顯示消息框,並使用屬性,以指示應顯示標籤:

public class MyWindow 
{ 
    public bool ShouldLabelBeDisplayed { get; set; } 

    public void CheckBox_Checked(object sender, RoutedEventArgs e) 
    { 
     DialogResult dialogResult = MessageBox.Show(...); 
     if (dialogResult == DialogResult.Yes) 
     { 
      ShouldLabelBeDisplayed = true; 
     } 
     else 
     { 
      ShouldLabelBeDisplayed = false; 
     } 
    } 

最後,你的標籤的Visible屬性綁定到這個屬性:

<Label Name="..." Visibility="{Binding Path=ShouldLabelBeDisplayed, Converter={StaticResource BoolToVisConverter}}" /> 

它可能不是這個,但你明白了。

+0

謝謝你,它的工作,還有一個問題,假設如果我有另一個UserControl2中的另一個標籤,如果我給這些屬性我有問題就像在MessageBox顯示之前顯示, – Kumar 2013-04-24 11:36:17

0

TRU使標籤的屬性:

yourlabel.Visibility="Hidden". 

然後,當你想展示,你應該讓物業:

yourlabel.Visibility="Visible".