簡體   English   中英

Windows 10 x:綁定到SelectedItem

[英]Windows 10 x:Bind to SelectedItem

我正在嘗試移植/采用我的Windows RT應用程序到WIndows10,我正在嘗試新的綁定x:Bind。

到目前為止,我能夠綁定到我的ViewModel屬性和其他Viewelements。 但現在我正在嘗試將TextBox的文本綁定到GridView的SelectedItem。

在經典綁定中,我就是這樣做的。

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

我正在嘗試

  • Text =“{x:Bind gv_textgroup.SelectedItem.bezeich,Mode = TwoWay}”
  • Text =“{x:Bind textgroup [gv_textgroup.SelectedIndex] .bezeich,Mode = TwoWay}”
    • textgroup是我的viewmodelclass,包含所有元素

但它沒有奏效......任何想法?

並且有人可以解釋我如何處理“DependencyProperty”。 我從“build 2015”中看到了viedo,並提供了示例代碼。 但它對我說什么......我是一個新手......

非常感謝您的幫助

我不確定為什么會這樣,但是如果你創建一個對象到對象的轉換器, 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;
    }
}

你可以像這樣使用它:

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

特別感謝runceel的公開樣本。

他用日語解釋了這里

您不能在GridView的SelectedItem上使用x:Bind。 這是因為SelectedItem是一個對象,所以它可以是任何東西。 x:綁定需要有實際的類/接口。 x:Bind不使用反射來查找像Binding這樣的屬性。

您可以通過x完成此操作:將GridView的SelectedItem綁定到視圖模型,然后將x:綁定到TextBlock中的那個。 我不確定這對你的表現有多大幫助。

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