다음과 같은 경우가 있습니다. 나는 문자열의 목록을 보여주고 싶다. 이를 달성하기 위해 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 빈 문자열을 제외한 모든 단일 항목. 내가 원하는 것을 어떻게 얻을 수 있습니까?