Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows 10 x:Bind to SelectedItem

I'm trying to port/adopt my Windows RT app to WIndows10 and I'm trying out the new bindings x:Bind.

So far I'm able to bind to my ViewModel properties and other Viewelements. But now I'm trying to bind the text of a TextBox to a SelectedItem of a GridView.

In classic binding I'm doing it like that.

<TextBox x:Name="tb_textgroup"
                             Grid.Row="1"
                             PlaceholderText="Change Groupname"
                             Text="{Binding UpdateSourceTrigger=PropertyChanged,
                                    ElementName=gv_textgroup,
                                    Mode=TwoWay,Path=SelectedItem.bezeich}"
                             IsEnabled="{Binding UpdateSourceTrigger=PropertyChanged,
                                       ElementName=gv_textgroup,
                                       Mode=TwoWay,Path=SelectedItem.edit_activated}"
                             Margin="20,10,20,0"
                             />

I was trying it with

  • Text="{x:Bind gv_textgroup.SelectedItem.bezeich, Mode=TwoWay}"
  • Text="{x:Bind textgroup[gv_textgroup.SelectedIndex].bezeich, Mode=TwoWay}"
    • where textgroup is my viewmodelclass with all the elements

But None of it worked... any ideas?

And can someone explain me what to do with "DependencyProperty". I watched the viedo from "build 2015" and have the sample codes. But it's saying nothing to me... I'm quite a newbie...

Many thanks for your help

like image 829
thezapper Avatar asked May 20 '15 19:05

thezapper


2 Answers

I'm not sure why this works, but if you create an object-to-object converter, x:Bind works for two-way conversion on any SelectedItem.

public class NoopConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return value;
    }
}

And you can use it like this:

<ListView ItemsSource="{x:Bind ViewModel.Items}"
         SelectedItem="{x:Bind ViewModel.SelectedItem, Mode=TwoWay, Converter={StaticResource NoopConverter}}"
         ...

Special thanks to runceel for his public samples.

He explains it here in Japanese.

like image 64
Laith Avatar answered Nov 03 '22 11:11

Laith


You cannot use x:Bind on the SelectedItem of a GridView. This is because the SelectedItem is an object, so it can be anything. x:Bind needs to have actual classes/interfaces. x:Bind does not use reflection to find properties like Binding does.

You can accomplish this by x:Bind the SelectedItem of the GridView to your view model and then x:Bind to that from the TextBlock. I'm not sure this would really help performance as much as you would like.

public class ViewModel
{
    public MyItem SelectedItem { get; set; } //fire prop changed
}

<GridView SelectedItem="{x:Bind SelectedItem, mode=Twoway}"/>
<TextBlock Text="{x:Bind ViewModel.SelectedItem.bezeich}"
like image 40
Shawn Kendrot Avatar answered Nov 03 '22 12:11

Shawn Kendrot