7

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

thezapper
  • 486
  • 4
  • 13

2 Answers2

11

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.

Laith
  • 6,071
  • 1
  • 33
  • 60
  • 3
    It works because when compiler sees there is a converter for the binding, it ignores the fact that there may be type conflicts and allows the code with the risk of a runtime exception. – Mehrzad Chehraz Nov 16 '16 at 07:00
3

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}"
Shawn Kendrot
  • 12,425
  • 1
  • 25
  • 41
  • I'm afraid you're right. But I think this is really a pitty that you can't cast from object to your class. ANyway I hope Microsoft will improve that technique as they mentioned on build (I tought I've read/heard it in a Video). Thanks for your time – thezapper Jun 11 '15 at 18:13
  • How can they possibly know what you want the object to be. x:Bind is a compile time directive. That means that it will create code at compile time that binds to your object/properties. You cannot expect the compiler to know that you want an object to be of type MyObject. – Shawn Kendrot Jun 11 '15 at 19:28
  • Maybe something like this: – thezapper Jun 12 '15 at 09:26
  • Actually, the viewmodel property can be a plain object... and it needs to be unless you're using a Converter. The DataTemplate's x:DataType says what type of class the object really is supposed to be. This doesn't change the fact that you can't x:bind to other xaml element properties. – Lee McPherson Jun 29 '15 at 14:29