Compiled Binding Two way

768 views Asked by At

I am trying to setup two way binding to a SelectedItem on a ListView using the new {x:Bind} syntax.

See the Code Below:

XAML

<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>

Code Behind

        public sealed partial class CategoriesEdit: Page
        {
            public CategoriesViewModel Vm
            {
                get
                {
                    return (CategoriesViewModel)this.DataContext;
                }
            }
            public CategoriesEdit()
            {
                this.InitializeComponent();
            }
        }

Everything works correctly when I do a one-way. But when I try and set SelectedItem to Two-way binding I get the following error:

Invalid binding path 'Vm.CurrentCategory' : Cannot bind type 'GJ_2.Domain.Category' to 'System.Object' without a converter

Any ideas would be great, there isn't much out there yet for this tech.

1

There are 1 answers

1
George Birbilis On BEST ANSWER

It is logical I think to require a converter at the two way binding since SelectedItem is of type object, not assignable to Vm.CurrentCategory

Could make that object too or add helper property that casts to/from object and bind to that, or make a converter and pass it as parameter to the binding (guess they still support such judging from the error message)