Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF-ReSharper无法识别XAML中继承的绑定作用域_Wpf_Xaml_Resharper - Fatal编程技术网

WPF-ReSharper无法识别XAML中继承的绑定作用域

WPF-ReSharper无法识别XAML中继承的绑定作用域,wpf,xaml,resharper,Wpf,Xaml,Resharper,我在XAML设计视图中使用ReSharper时遇到问题。以下是我的源代码: main window.xaml <Window x:Class="MyNamespace.Views.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

我在XAML设计视图中使用ReSharper时遇到问题。以下是我的源代码:

main window.xaml

<Window x:Class="MyNamespace.Views.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:system="clr-namespace:System;assembly=mscorlib"
        xmlns:vm="clr-namespace:MyNamespace.ViewModels">
    <Window.DataContext>
        <vm:MainWindowViewModel/>
    </Window.DataContext>
    <Grid>
        <StackPanel>

            <!-- User selects item from list -->
            <ListView ItemsSource="{Binding Items}" IsSynchronizedWithCurrentItem="True">
                <ListView.Resources>
                    <DataTemplate DataType="{x:Type Item}">
                        <TextBlock Text="{Binding Name}">
                    </DataTemplate>
                </ListView.Resources>
            </ListView>

            <!-- Currently selected list item details are shown here -->
            <ContentControl Content="{Binding Items}">
                <ContentControl.ContentTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock Text="{Binding Description}"/>
                        </StackPanel>                       
                    </DataTemplate>
                </ContentControl.ContentTemplate>
            </ContentControl>

        </StackPanel>
    </Grid>
</Window>

C#类

public class MainWindowViewModel
{
    public ObservableCollection<Item> Items { get; set; }

    public MainWindowViewModel()
    {
        Items = new ObservableCollection<Item>();
    }

    // ...
}

public class Item
{
    public string Name { get; set; }
    public string Description { get; set; }

    // ...
}
公共类MainWindowViewModel
{
公共ObservableCollection项{get;set;}
公共主窗口视图模型()
{
Items=新的ObservableCollection();
}
// ...
}
公共类项目
{
公共字符串名称{get;set;}
公共字符串说明{get;set;}
// ...
}
ReSharper在my
ContentControl
XAML元素的
中的说明下划线,警告文本为
无法解析类型为“MyNamespace.ViewModels.MainWindowViewModel”的数据上下文中的属性“Description”


我不确定它是如何变得混乱的,因为我已经在
窗口中指定了我的DataContext
元素,所以它肯定知道我使用的是什么类。而且,我的应用程序运行时没有问题,功能正常,因此我确信这是一个重新精简问题。

如果我没有错,当MainWindowViewModel位于不同的类(/namespace)中时,Description属性在DataContext中不可用。 尝试如下设置DataTemplate的数据类型


您可能需要导入clr名称空间,但如果需要,ReSharper应该建议正确的名称空间。

这解决了问题!我意识到我在我的
ListView
中指定了
DataType
,但在我的
ContentControl
中没有指定,这解释了为什么ReSharper只在一个地方抱怨。谢谢大家!@基列夫。哦,我没有看到您也在资源中设置了数据类型,但是ContentControl中仍然需要它。。没问题,欢迎你(: