Windows 10 x:Bind to SelectedItem

懵懂的女人 提交于 2019-12-07 03:57:14

问题


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


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/30359025/windows-10-xbind-to-selecteditem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!