【问题标题】:How do I specify a DataGrid's ItemsSource type in XAML?如何在 XAML 中指定 DataGrid 的 ItemsSource 类型?
【发布时间】:2017-01-03 18:59:57
【问题描述】:

我将DataGrid 绑定到ICollectionView,以便我可以有效地过滤ItemsSource,但ICollectionView 不是generic 类型(如CollectionView<MyType>) - 它是@987654327 类型@。所以在 XAML 编辑器中,VisualStudio 无法确定类型是什么,所以我没有得到任何 IntelliSense 帮助绑定到集合视图中对象的属性。它仍然可以构建和运行,但我在设计时没有得到帮助。

重新表述问题:无论如何要在 XAML 中“强制转换”数据绑定?

我想我可以用 <DataGrid.DataContext> 做点什么,但我不记得它是什么,我也没有运气在谷歌上搜索它:

XAML:

<DataGrid ItemsSource="{Binding MyCollectionView}">
    <DataGrid.DataContext>
        <!-- Specify the type of objects in MyCollectionView somehow -
                 something like 'x:type="MyType"' -->
    </DataGrid.DataContext>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <!-- Cannot resolve property 'Approved' in data context of type 'MyProject.MainWindow'. -->
                <DataTrigger Binding="{Binding Approved}" Value="False">
                    <Setter Property="Background" Value="LightGray" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
    <DataGrid.Columns>
        <!-- Cannot resolve property 'Approved' in data context of type 'object'. -->
        <DataGridTextColumn Header="Is Approved"
                            Binding="{Binding Approved}"
                            Width="3*" />
    </DataGrid.Columns>
</DataGrid>

代码背后:

public partial class MainWindow : Window, INotifyPropertyChanged
{
    public ICollectionView MyCollectionView { get; set; }

    public MainWindow(List<MyType> parameter)
    {
        // ...

        MyCollectionView = new CollectionView(parameter);

        // ...
    }
}

public class MyType
{
    public bool Approved { get; set; }

    // ...
}

【问题讨论】:

  • 您说明了为什么它在设计时不起作用,但您是说绑定在运行时失败而应该起作用?
  • "ICollectionView 不是泛型类型 - 它是 List 类型" List&lt;object&gt; 是泛型集合。我们能否看到MyCollectionView 是如何定义的,以及集合本身是如何定义的?集合中的对象是否真的有一个名为Approved 的属性? “公认”是什么意思?
  • @OmegaMan 不,它仍在构建和运行。我的问题是如何在设计时指定ItemsSource 实际上是List&lt;MyType&gt;,而不是List&lt;object&gt; - 获得随之而来的所有智能感知和类型检查优势。我更新了我的问题以解决您的评论。
  • @EdPlunkett ICollectionView 在“泛型”的 C# 意义上不是泛型(ICollectionView 是 .NET 类型)。 “未识别”是指我在“已批准”下收到一条波浪状的建议,消息显示Cannot resolve property 'Approved' in the context of type 'MyProject.MainWindow'.。我更新了我的问题以解决您的评论。
  • 由于这是一个设计时问题,请将您的 Visual Studio 版本作为标签。

标签: c# wpf xaml data-binding visual-studio-2015


【解决方案1】:

我以为我可以用 做一些事情,但我不记得它是什么,而且我也没有任何运气在谷歌上搜索它:

我相信设置设计时数据上下文是您正在寻找的。请参阅以下链接了解更多信息。

XAML:用于绑定和数据上下文的智能感知:https://blogs.msmvps.com/deborahk/xaml-intellisense-for-bindings-and-the-data-context/ How to see design-time data-binding in XAML editor (it works in runtime)?

我想问的是,是否有任何方法可以将数据绑定“转换”为 XAML 中的 MyType 集合?

没有。但您可以如上所述指定设计时 DataContext。

【讨论】:

  • MS MVP 链接有我正在寻找的内容:d:DataContext="{d:DesignInstance Type=vms:CustomerListViewModel, IsDesignTimeCreatable=True}"
【解决方案2】:

VisualStudio 无法确定类型是什么

由于类型直到运行时才被表达,并且是通过代码使用反射获得的,所以设计者处于劣势,只能根据它知道的内容进行推断。它知道的只是它是一个object,但不是开发人员知道的确切类型。

我没有得到任何 IntelliSense 帮助...仍在构建和运行,但我在设计时没有得到帮助

如果这是阻碍您的问题,我建议您暂时将相关属性类型的列表添加到 VM(或页面,如果不是 MVVM),然后绑定到该新属性。然后设计时将查看它需要什么,您可以在 Visual Studio 的帮助下获取属性信息添加绑定/样式。

一旦一切顺利,将设计时绑定替换为您提到的ICollection

【讨论】:

  • 是的,我明白这一点 - 我知道问题出在哪里。我想问的是,是否有任何方法可以将数据绑定“转换”为 XAML 中 MyType 的集合?
  • @tehDorf,据我所知,不,没有办法将其转换为 XAML。
猜你喜欢
  • 2011-03-03
  • 2011-11-26
  • 2011-03-22
  • 2013-01-14
  • 1970-01-01
  • 1970-01-01
  • 2014-12-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多