2011-12-09 8 views
-3

これを行う正しい方法は何ですか?この方法では動作しません。how ... ifステートメントとボタンコンテンツ "WP7"

if (((String)enterAmountButton.Content) == "") 
MessageBox.Show("Please enter the total bill amount."); 
else if (((String)enterTipButton.Content) == "") 
MessageBox.Show("Please enter the tip % amount."); 

この方法は、どちらも機能していません。

if (enterAmountButton.Content == "") 
MessageBox.Show("Please enter the total bill amount."); 
else if (enterTipButton.Content == "") 
MessageBox.Show("Please enter the tip % amount."); 
+0

投稿する前にデバッグしてみてください。コンテンツの種類がわかり、文字列に適切にキャストできると思います。 –

答えて

-1
var amountButtonString = enterAmountButton.Content as string; 
     var enterTipButtonString = enterTipButton.Content as string; 
     if (String.IsNullOrEmpty(amountButtonString)) 
      MessageBox.Show("Please enter the total bill amount."); 
     else if (String.IsNullOrEmpty(enterTipButtonString)) 
      MessageBox.Show("Please enter the tip % amount."); 

が働くだろう。しかし、どのように文字列を取得する予定ですか?ボタンの隣にあるTextBoxの値が正しいと思われますか?その場合の

amountTextBox & tipTextBoxがあなたのTextBoxesx:Nameある

if (String.IsNullOrEmpty(amountTextBox.Text)) 
       MessageBox.Show("Please enter the total bill amount."); 
      else if (String.IsNullOrEmpty(tipTextBox.Text)) 
       MessageBox.Show("Please enter the tip % amount."); 

最後のもの:

おそらくこれを処理するより良い方法があります。たとえば、テキストボックスにkeyUpイベント& TextChangedイベントを処理し、テキストのみが存在する場合、ボタンを有効にした場合(、より良い有効なテキストを;))

あなたも、コンバータを使用することができます。

public class StringToEnabledConverter : IValueConverter 
    { 
     #region IValueConverter Members 

     public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      var val = value as string; 
      if (val == null) 
       throw new ArgumentException("value must be a string."); 

      return !string.IsNullOrEmpty(val); 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
     { 
      throw new NotImplementedException(); 
     } 

     #endregion 
    } 

追加しますあなたのコンバータへの参照<Converters:StringToEnabledConverter x:Key="StringToEnabledConverter" />

と、ボタンに、

<TextBox x:Name="amountTextBox /> 
<Button x:Name="enterAmountButton" Enabled="{Binding ElementName=amountTextBox, Path=Text, Converter={StaticResource StringToEnabledConverter}}" /> 
+0

ボタンで何をしているのですか?ページが読み込まれると、ボタンのテキストは空になります。ユーザがボタンをクリックすると、ユーザは第2ページに向けられ、ここでいくつかの数字が入力される。彼が前のページに戻ると、それらの数字がボタンテキストに表示されます。私はちょうど各テキストにデータを入力するようにしたいと思うし、そうでなければ、テキストがないボタンにデータを入力するように指示するメッセージを表示したい。 – iamrelos

+0

@ Twenty40コードの最初の部分で何が問題なのですか? –

+0

ありがとうwillmel ...あなたの最初の提案が働いた.. – iamrelos

0
if ((string)(enterAmountButton.Content) == "") 
MessageBox.Show("Please enter the total bill amount."); 
else if ((string)(enterTipButton.Content) == "") 
MessageBox.Show("Please enter the tip % amount."); 

または代わり

if (enterAmountButton.Content.ToString() == "") 
MessageBox.Show("Please enter the total bill amount."); 
else if (enterTipButton.Content.ToString() == "") 
MessageBox.Show("Please enter the tip % amount."); 
+0

最初の解決策は、Contentが文字列にキャストできると仮定します。できない場合は、String.Emptyの代わりに 'ToString()'を使用する必要があります。 –

0

このような何か試してみてください:

if(enterAmountButton.Content.ToString().Trim() == String.Empty) 
MessageBox.Show("Please enter the total bill amount."); 
else if (enterTipButton.Content.ToString().Trim() == String.Empty) 
MessageBox.Show("Please enter the tip % amount."); 

Trim()を任意の先頭または末尾の空白がないことを保証します。多分あなたの最初の試行がうまくいかなかったのはなぜですか?

+0

string.IsNullOrWhiteSpace(enterAmountButton.Content.ToString()。Trim())を使用できます。 –