Data binding Windows 8列表框数据绑定
我有一个列表框,我正试图用“metro”应用程序进行数据绑定。这是我的xaml:Data binding Windows 8列表框数据绑定,data-binding,windows-8,listbox,Data Binding,Windows 8,Listbox,我有一个列表框,我正试图用“metro”应用程序进行数据绑定。这是我的xaml: <ListBox x:Name="ImagesList" Margin="40" Grid.Row="1"> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Key}" />
<ListBox x:Name="ImagesList" Margin="40" Grid.Row="1">
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Key}" />
</StackPanel>
</DataTemplate>
</ListBox>
看起来它正在显示我绑定的类型的全名。。。我做错了什么?您需要为
绑定指定一个转换器
作为XAML资源的cerate转换器
<src:KeyValueConverter:Key="KeyConverter"/>
示例转换器代码
public class KeyValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var kvp = (KeyValuePair)value;
return kvp.Key;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
我的Xaml也错了,应该是:
<ListBox x:Name="ImagesList" Margin="40" Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Value}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
我将在何处将其标记为答案,因为我相信它适用于我的KVP示例。
Text="{Binding Path=ItemsList, Converter={StaticResource KeyConverter}}"
public class KeyValueConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var kvp = (KeyValuePair)value;
return kvp.Key;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<ListBox x:Name="ImagesList" Margin="40" Grid.Row="1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Value}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>