简体   繁体   English

Windows 10 x:绑定到SelectedItem

[英]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. 我正在尝试移植/采用我的Windows RT应用程序到WIndows10,我正在尝试新的绑定x:Bind。

So far I'm able to bind to my ViewModel properties and other Viewelements. 到目前为止,我能够绑定到我的ViewModel属性和其他Viewelements。 But now I'm trying to bind the text of a TextBox to a SelectedItem of a GridView. 但现在我正在尝试将TextBox的文本绑定到GridView的SelectedItem。

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 gv_textgroup.SelectedItem.bezeich,Mode = TwoWay}”
  • Text="{x:Bind textgroup[gv_textgroup.SelectedIndex].bezeich, Mode=TwoWay}" Text =“{x:Bind textgroup [gv_textgroup.SelectedIndex] .bezeich,Mode = TwoWay}”
    • where textgroup is my viewmodelclass with all the elements textgroup是我的viewmodelclass,包含所有元素

But None of it worked... any ideas? 但它没有奏效......任何想法?

And can someone explain me what to do with "DependencyProperty". 并且有人可以解释我如何处理“DependencyProperty”。 I watched the viedo from "build 2015" and have the sample codes. 我从“build 2015”中看到了viedo,并提供了示例代码。 But it's saying nothing to me... I'm quite a newbie... 但它对我说什么......我是一个新手......

Many thanks for your help 非常感谢您的帮助

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 . 我不确定为什么会这样,但是如果你创建一个对象到对象的转换器, x:Bind可以在任何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. 特别感谢runceel的公开样本。

He explains it here in Japanese. 他用日语解释了这里

You cannot use x:Bind on the SelectedItem of a GridView. 您不能在GridView的SelectedItem上使用x:Bind。 This is because the SelectedItem is an object, so it can be anything. 这是因为SelectedItem是一个对象,所以它可以是任何东西。 x:Bind needs to have actual classes/interfaces. x:绑定需要有实际的类/接口。 x:Bind does not use reflection to find properties like Binding does. x:Bind不使用反射来查找像Binding这样的属性。

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. 您可以通过x完成此操作:将GridView的SelectedItem绑定到视图模型,然后将x:绑定到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}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM