我正在尝试使用新的{x:Bind}语法在ListView上设置双向绑定到SelectedItem。
请参阅下面的代码:
XAML
bluetooth.enable()
代码背后
<Page
x:Class="GJ_2.Views.CategoriesEdit"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:GJ_2"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:domain="using:GJ_2.Domain"
mc:Ignorable="d" DataContext="{Binding CategoriesViewModel, Source={StaticResource Locator}}"
>
<Page.Resources>
<DataTemplate x:Key="CategoryTemplate" x:DataType="domain:Category">
<Grid Margin="0,0,0,14">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="250" />
<ColumnDefinition Width="50" />
<ColumnDefinition Width="50" />
</Grid.ColumnDefinitions>
<Image></Image>
<StackPanel Grid.Column="1">
<TextBlock Text="{x:Bind Title}" FontSize="18.667" FontWeight="Bold" TextWrapping="Wrap" />
<TextBlock Text="{x:Bind Description}" />
</StackPanel>
<SymbolIcon Symbol="Up" Grid.Column="2" ></SymbolIcon>
<AppBarButton Grid.Column="4" Foreground="White">
<AppBarButton.Icon>
<PathIcon HorizontalAlignment="Center" VerticalAlignment="Center" Data="M9.99967861175537,0L15.0010814666748,4.41280698776245 20,8.82655048370361 13.8758192062378,8.82655048370361 13.8758192062378,20 6.12418174743652,20 6.12418174743652,8.82655048370361 0,8.82655048370361 4.9995322227478,4.41280698776245 9.99967861175537,0z" />
</AppBarButton.Icon>
</AppBarButton>
</Grid>
</DataTemplate>
</Page.Resources>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" >
<CommandBar VerticalAlignment="Bottom">
<AppBarButton Icon="Add" Label="Add" />
<AppBarButton Icon="Edit" Label="Edit"/>
<AppBarButton Icon="Delete" Label="Delete" />
</CommandBar>
<ListView ItemsSource="{x:Bind Vm.CategoriesList}" ItemTemplate="{StaticResource CategoryTemplate}" SelectedItem="{x:Bind Vm.CurrentCategory, Mode=TwoWay}">
</ListView>
</Grid>
</Page>
当我单向行动时,一切正常。但是当我尝试将SelectedItem设置为双向绑定时,我收到以下错误:
无效的绑定路径&#39; Vm.CurrentCategory&#39; :无法绑定类型&#39; GJ_2.Domain.Category&#39;到&#39; System.Object&#39;没有转换器
任何想法都会很棒,但对于这项技术还没有多少想法。
答案 0 :(得分:0)
我认为在双向绑定中需要转换器是合乎逻辑的,因为SelectedItem属于object类型,不能赋予Vm.CurrentCategory
可以创建该对象,也可以添加从对象转换为对象并绑定到该对象的辅助属性,或者创建转换器并将其作为参数传递给绑定(猜测它们仍然支持从错误消息判断)