好的,这更多的是烦恼而不是问题。没有错误

页面

<ContentPage
   ...
   x:Name="This"
   //hack to have typed xaml at design-time
   BindingContext="{Binding Source={x:Static viewModels:ViewModelLocator.ChooseTargetLocationVm}}"

subview
<views:ProductStandardView
    ...
    BindingContext="{Binding Product}">
    <Grid.Triggers>
        <DataTrigger
            Binding="{Binding Path=BindingContext.IsVacate, Source={x:Reference This}}"
            TargetType="Grid"
            Value="true">
            <Setter Property="BackgroundColor" Value="{StaticResource WarningColor}" />
        </DataTrigger>
    </Grid.Triggers>

当从BindingContext的源引用绑定(bind)到This时,我得到一个XAML“警告”


Binding="{Binding Path=BindingContext.IsVacate, Source={x:Reference This}}"

显然,BindingContext是一个对象,并且没有类型。 但是上面的代码可以编译和运行

我要执行的操作是强制转换,首先是因为我拥有OCD,但是主要是因为它易于在IDE页面 channel 栏上发现实际问题

以下内容似乎合乎逻辑,但不起作用
Binding="{Binding Path=BindingContext.(viewModels:ChooseTargetLocationVm.IsVacate),
                  Source={x:Reference This}}"

在输出中我得到



我了解错误,但我还能如何转换?

只是出于愚蠢,显然以下内容不会编译
Binding="{Binding Path=((viewModels:ChooseTargetLocationVm)BindingContext).IsVacate, Source={x:Reference This}}"

那么,有没有一种方法可以将BindingContext强制转换为ViewModel,以便在设计时键入任何SubProperty引用?

更新

这与DataTemplate内部有关,或者在这种情况下,当控件具有自己的BindingContext时,这就是为什么我需要使用Source={x:Reference This}定位页面的原因。

注意:<ContentPage.BindingContext>对我不起作用,因为我使用的是棱镜和统一性,并且在初始测试中它似乎不能很好地与默认构造函数配合使用,尽管我可能还会对此进行一些尝试

最佳答案

您可以扩展ContentPage来创建通用类型-支持 View 模型的类型参数-进而可以在Binding标记扩展中使用。

尽管它可能不会给您提供类似的智能感知功能-但一定要为您删除警告。

例如:

/// <summary>
/// Create a base page with generic support
/// </summary>
public class ContentPage<T> : ContentPage
{
    /// <summary>
    /// This property basically type-casts the BindingContext to expected view-model type
    /// </summary>
    /// <value>The view model.</value>
    public T ViewModel { get { return (BindingContext != null) ? (T)BindingContext : default(T); } }

    /// <summary>
    /// Ensure ViewModel property change is raised when BindingContext changes
    /// </summary>
    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();

        OnPropertyChanged(nameof(ViewModel));
    }
}

sample 用法
<?xml version="1.0" encoding="utf-8"?>
<l:ContentPage
    ...
    xmlns:l="clr-namespace:SampleApp"
    x:TypeArguments="l:ThisPageViewModel"
    x:Name="This"
    x:Class="SampleApp.SampleAppPage">

    ...
         <Label Text="{Binding ViewModel.PropA, Source={x:Reference This}}" />
    ...
</l:ContentPage>

背后的代码
public partial class SampleAppPage : ContentPage<ThisPageViewModel>
{
    public SampleAppPage()
    {
        InitializeComponent();

        BindingContext = new ThisPageViewModel();
    }
}

查看模型
/// <summary>
/// Just a sample viewmodel with properties
/// </summary>
public class ThisPageViewModel
{
    public string PropA { get; } = "PropA";
    public string PropB { get; } = "PropB";
    public string PropC { get; } = "PropC";

    public string[] Items { get; } = new[] { "1", "2", "3" };
}

关于c# - 强制转换绑定(bind)路径,以便在设计时识别ViewModel属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51412780/

10-10 15:28