2012-05-16 7 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

내가 꽤 질문을 이해 할 수는 없지만 가시성을 결정하는 당신의 ViewModel에 부울을 추가 할 수 있습니다? 그런 다음 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

문자열이 비어의 null가 아닌 경우, 당신은 그냥 확인하고 싶은 경우 사용

string 클래스에 내장 된 IsNullOrEmpty 정적 방법이 있습니다
if(!string.IsNullOrEmpty(value)) 
    { 
     //// 
    } 
2

, 그 사용

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.IsNullOrEmpty(input)) 
      { 
       return Visibility.Collapsed; 
      } 
      else 
      { 
       return Visibility.Visible; 
      } 
     } 

     return Visibility.Visible; 
    } 

} 
+0

나는이 일들 중 하나를 수행하기에 충분하지 않은 담당자에게 upvote하고 rep 할 것이지만, 미안하다! –

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; 
    } 
관련 문제