Windows 10 x: snap to selected item

I am trying to connect / apply a Windows RT application to WIndows10 and I am trying to use the new x: Bind bindings.

So far, I can bind to my ViewModel properties and other View elements. But now I'm trying to bind TextBox text to a SelectedItem GridView.

In classic binding, I do it like this.

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

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

But none of them worked ... any ideas?

And can someone explain to me what to do with "DependencyProperty". I watched viedo with "build 2015" and had code samples. But that doesnโ€™t tell me anything ... I'm pretty newbie ...

Many thanks for your help

+6
source share
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 as follows:

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

Special thanks to runceel for his publicly available samples.

He explains it here in Japanese.

+11
source

You cannot use x: bind to the selected GridView element. This is because SelectedItem is an object, so it can be anything. x: The binding must have actual classes / interfaces. x: Binding does not use reflection to search for properties such as Binding.

You can accomplish this with x: bind the selected GridView to your view model, and then x: bind to the TextBlock. I am not sure if this will 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}" 
+3
source

Source: https://habr.com/ru/post/987605/


All Articles