2013-02-22 83 views
3

我们使用的是Resharper,当然我们想要利用Resharper的xaml intellisense。Resharper dosn't无法识别正确的ViewModel类型

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

我已经添加在视图模型的线来设置正确的类型:

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

但ReSharper的还是不断在寻找ViewModelbase的属性。

我还可以尝试什么?

更多的代码:

设置的DataContext:

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

绑定的东西(产品是ReportFilterViewmodel一个物业,R#保持在ViewModelBase寻找它):

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

能否请您提供更大的代码示例(与绑定表达式本身)? R#应处理您提供的设计时属性。 – ControlFlow 2013-02-22 11:40:01

+1

请参阅我添加的新代码部分。 – TheJoeIaut 2013-02-22 12:09:45

+1

您是否按照规定在xaml中同时设置了DesignInstance和真正的DataContext? ReSharper可能正在查看正在设置的显式DataContext,而不是DesignInstance。 – 2013-02-22 12:13:17

回答

2

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>