2017-09-11 1 views
0

다음과 같은 경우가 있습니다. 나는 문자열의 목록을 보여주고 싶다. 이를 달성하기 위해 ListView를 문자열 컬렉션에 바인딩했습니다. 이 컬렉션에는 빈 문자열이 있습니다. 내가 원하는 것은 빈 문자열이있을 때 다음 텍스트를 보여주는 것입니다 : "-empty-". 이것은 내가 지금까지 무엇을 가지고 있습니다 (소스 코드 데모 목적으로 만 제공됩니다) :UWP, ListView의 항목 소스가 문자열 모음 인 경우 ListView의 항목에 변환기 적용

EmptyStringConverter.cs

public class EmptyStringConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, string language) 
    { 
     if (value is string && string.IsNullOrWhiteSpace((string)value)) 
     { 
      return "-empty-"; 
     } 
     return value; 
    } 

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

에서 MainPage.xaml

<Page 
     x:Class="App1.MainPage" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="using:App1" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     mc:Ignorable="d"> 

    <Page.Resources> 
     <local:EmptyStringConverter x:Key="EmptyStringConverter" /> 
    </Page.Resources> 

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 

     <ListView x:Name="ListView"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <TextBlock Text="{Binding Converter={StaticResource EmptyStringConverter}}" Margin="0,0,0,5" /> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 
    </Grid> 
</Page> 

MainPage.xaml.cs를

public sealed partial class MainPage : Page 
{ 
    public MainPage() 
    { 
     this.InitializeComponent(); 
     var source = new[] { "", "String 1", "String 2" }; 
     ListView.ItemsSource = source; 
    } 
} 

EmptyStringConverter 클래스의 Convert 메서드에 중단 점을 넣을 때이 메서드는 i n 빈 문자열을 제외한 모든 단일 항목. 내가 원하는 것을 어떻게 얻을 수 있습니까?

답변

0

글쎄, 문제는 내가 마지막으로 확인하던 곳이었습니다. 문제를 일으키는 것은 Legacy Binding입니다. 코드 스 니펫을 직접 시도한 다음 조각으로 교체했습니다. 당신이 UWP을 사용하고 있기 때문에 아래의 라인은 당신이 당신의 문제를 해결할 수 있습니다하는 Compile time binding로 전환 할 수 있습니다 legacy binding

Text="{Binding Converter={StaticResource EmptyStringConverter}}"

를 사용하여 ListViewXAML는 것 수정 :

<ListView x:Name="ListView" > 
     <ListView.ItemTemplate> 
      <DataTemplate x:DataType="x:String"> 
       <TextBlock Text="{x:Bind Converter={StaticResource EmptyStringConverter},Mode=OneTime}" Margin="0,0,0,5" /> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
</ListView> 

두 가지에주의하십시오 사물 :

  1. Text 대신 x:Bind을 사용합니다. 레거시가 Binding 인 경우 기본적으로 OneWay 바인딩 인 컴파일 시간 바인딩은 기본적으로 oneTime 바인딩입니다 (모드를 명시 적으로 언급하지 않는 한). 컴파일 시간 바인딩에 대한 자세한 내용은 xBind markup extension을 참조하십시오.
  2. 선언에 DataType 속성이 있으면 컴파일러가 예상 한 데이터 종류를 컴파일하는 데 도움이됩니다. DataType에 대한 자세한 내용은 xBind markup extension을 참조하십시오.

모든 즉, 내가보기 엔 새로운 컴파일 시간, 변환 많이 적은 코드와 노력으로 이어지는 바인딩 엔진 자체에 의해 처리됩니다 바인딩으로 대신 ListView.ItemSource=sourceData Binding 접근 방식을 사용하는 것이 좋습니다 것이라고 말했다되고 너 끝. 같은 것을 위해 github에 샘플을 올려 놓았습니다. 체크 아웃 할 수 있습니다 : EmptyStringDemo