2012-05-16 59 views
0

好吧,所以我目前正在嘗試使複選框基於字符串是否可以很快或不行,但是我的網格中每行的數據將每次都不同,因此我無法將其設置爲檢查一個特定的字符串,我正在考慮檢查該字符串是否不爲空或空,但我不知道如何做到這一點,我在我的代碼中的錯誤if(string.Equals行,因爲我不確定如何完成這一關基於字符串c顯示#

public class StringToVisibilityConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (value != null && value is string) 
     { 
      var input = (string)value; 
      if (string.Equals 
      { 
       return Visibility.Collapsed; 
      } 
      else 
      { 
       return Visibility.Visible; 
      } 
     } 

     return Visibility.Visible; 
    } 
+0

我不是很明白的問題,但您可以加入一個布爾值,您的視圖模型決定了知名度?然後有一個BooleanToVisibilityConverter。 –

回答

1

.NET 4.0:

if (string.IsNullOrWhitespace(myString)) 

.NET預4.0:

if (string.IsNullOrEmpty(myString)) 

雖然,我想不同的寫邏輯(不需要進行一些檢查):

var input = value as string; 
if (input == null || string.IsNullOrWhiteSpace(input)) 
{ 
    return Visibility.Collapsed; 
} 
else 
{ 
    return Visibility.Visible; 
} 
1

如果你只是想檢查一個字符串是不是空的空然後使用:

if(!string.IsNullOrEmpty(value)) 
    { 
     //// 
    } 
2

有內置於stringIsNullOrEmpty靜態方法,使用:

​​
+0

我會upvote和代表,但沒有足夠的代表自己做這些事情之一,對不起! –

0

您可以使用string.IsNullOrEmpty

if (string.IsnullOrEmpty(input)) 
{ 
    return Visibility.Collapsed; 
} 
else 
{ 
    return Visibility.Visible; 
} 

如果您還希望包括空白頻段,使用string.IsNullOrWhiteSpace(> = .NET 4.0)。

0

使用String.IsNullOrEmpty

public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     if (!string.IsNullOrEmpty(value as string)) 
     { 
      return Visibility.Collapsed; 
     } 

     return Visibility.Visible; 
    }