如何在{x:bind}中强制转换依赖项属性

本文关键字:转换 依赖 属性 bind | 更新日期: 2023-09-27 18:22:12

{X:bind}的文档提到可以在属性路径中进行强制转换(例如{x:Bind obj.(TextBox.Text)})。在下面的简单示例中,我看不出这应该如何工作。我在parens中尝试了各种类型名称的组合,但都没有成功。

一个常见的用例是组合框的双向绑定,这里我将把SelectedValue属性从泛型对象类型转换为更具体的类型。

示例xaml页面MainPage.xam:

<Page
    x:Class="XBindTest4.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:XBindTest4"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" >
    <StackPanel>
        <ComboBox ItemsSource="{x:Bind SampleList, Mode=OneWay}" 
                  SelectedValue="{x:Bind SampleData, Mode=TwoWay}"/>
    </StackPanel>
</Page>

MainPage.xaml.cs:后面的代码

using System.Collections.ObjectModel;
using Windows.UI.Xaml.Controls;
namespace XBindTest4 {
    public class SampleClass {
    }
    public sealed partial class MainPage : Page {
        public ObservableCollection<SampleClass> SampleList = new ObservableCollection<SampleClass>(new[] {
            new SampleClass(),
            new SampleClass()
            });
        public SampleClass SampleData { get; set; }
        public MainPage() {
            this.InitializeComponent();
        }
    }
}

如何在{x:bind}中强制转换依赖项属性

目前,以现有的语法可能性,这是不可能的。这很糟糕,因为它向绑定表达式添加了不必要的重复代码:要在这种情况下使用{x:Bind},必须指定一个转换器,即使它什么都不做。

...
SelectedValue="{x:Bind SampleData, Mode=TwoWay, Converter={StaticResource NoOpConverter}}
...

如果使用转换器,则将适当的转换插入到生成的文件MainPage.g.cs:中

this.dataRoot.SampleData = (global::XBindTest4.SampleClass)this.LookupConverter("NoOpConverter").ConvertBack((this.obj2).SelectedValue, typeof(global::XBindTest4.SampleClass), null, null);

其他来源:

NoOpConverter.cs:

using System;
using Windows.UI.Xaml.Data;
namespace XBindTest4 {
    public class NoOpConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, string language)
            => value;
        public object ConvertBack(object value, Type targetType, object parameter, string language)
            => value;
    }
}

App.xam:中注册转换器

<Application
    x:Class="XBindTest4.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:XBindTest4"
    RequestedTheme="Light">
    <Application.Resources>
        <ResourceDictionary>
            <local:NoOpConverter x:Key="NoOpConverter"/>
        </ResourceDictionary>
    </Application.Resources>
</Application>

主页.xaml.cs

<Page
    x:Class="XBindTest4.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:XBindTest4"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" >
    <StackPanel>
        <ComboBox ItemsSource="{x:Bind SampleList, Mode=OneWay}" 
                  SelectedValue="{x:Bind SampleData, Mode=TwoWay, Converter={StaticResource NoOpConverter}}"/>
    </StackPanel>
</Page>