4

私たちはResharperを使用しており、もちろんResharperのxamlインテリセンスを利用したいと考えています。

ビューのデータコンテキストはCurrentViewmodel、タイプのプロパティにバインドされていますViewModelBase。実行時に、このプロパティはから継承するビューモデルで設定されViewModelBaseます。

正しいタイプを設定するために、ビューモデルにこれらの行をすでに追加しました。

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>

何かをバインドする(ProductsはReportFilterViewmodelのプロパティであり、r#はViewModelBaseでそれを探し続けます):

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

1 に答える 1

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>
于 2013-02-25T13:46:02.180 に答える