Resharper dosn';无法识别正确的ViewModel类型

本文关键字:ViewModel 类型 识别 dosn Resharper | 更新日期: 2023-09-27 18:00:27

我们正在使用Resharper,当然我们想利用Resharper的xaml intellisense。

我们的视图的数据上下文绑定到类型为ViewModelBaseCurrentViewmodel属性。在运行时,使用继承自ViewModelBase的视图模型设置此属性。

我已经在视图模型中添加了这些行,以设置正确的类型:

xmlns:vms="clr-namespace:PQS.ViewModel.Report"
d:DataContext="{d:DesignInstance vms:ReportFilterViewModel, IsDesignTimeCreatable=False}"

但Resharper仍在ViewModelbase中查找属性。

我还能试试什么?

更多代码:

设置数据上下文:

<UserControl.DataContext>
    <Binding Path="ReportMainViewModel.CurrentVm"  Source="{StaticResource Locator}"/>
</UserControl.DataContext>

绑定某物(Products是ReportFilterViewmodel上的一个属性,r#一直在ViewModelBase中查找它):

<ListBox   ItemsSource="{Binding Products.View}" Background="White" DisplayMemberPath="Name.ActualTranslation">
                    </ListBox>

Resharper dosn';无法识别正确的ViewModel类型

R#无法静态地找到运行时可用的具体视图模型类型,因此您需要手动注释数据上下文类型,如下所示:

using System.Collections.Generic;
public partial class MainWindow {
  public MainWindow() {
    Current = new ConcreteViewModel {
      Products = {
        new Product(),
        new Product()
      }
    };
    InitializeComponent();
  }
  public ViewModelBase Current { get; set; }
}
public class ViewModelBase { }
public class ConcreteViewModel : ViewModelBase {
  public ConcreteViewModel() {
    Products = new List<Product>();
  }
  public List<Product> Products { get; private set; }
}
public class Product {
  public string ProductName { get { return "Name1"; } }
}

和XAML部分:

<Window x:Class="MainWindow" x:Name="MainWin"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:global="clr-namespace:" mc:Ignorable="d"
        DataContext="{Binding ElementName=MainWin, Path=Current}">
  <!-- here the type of data context is ViewModelBase -->
  <Grid d:DataContext="{d:DesignInstance global:ConcreteViewModel}">
    <!-- and here is ConcreteViewModel -->
    <ListBox ItemsSource="{Binding Path=Products}">
      <ListBox.ItemTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding ProductName}"/>
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
  </Grid>
</Window>

或者像这样:

<Window x:Class="MainWindow" x:Name="MainWin"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:global="clr-namespace:"
        DataContext="{Binding ElementName=MainWin, Path=Current}">
  <Grid>
    <ListBox ItemsSource="{Binding Path=(global:ConcreteViewModel.Products)}">
      <ListBox.ItemTemplate>
        <DataTemplate>
          <TextBlock Text="{Binding ProductName}"/>
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
  </Grid>
</Window>