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
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
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.
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}"
链接地址: http://www.djcxy.com/p/44680.html
上一篇: 将标志枚举绑定到控件并返回枚举属性