简体   繁体   English

如何在XAML中指定DataGrid的ItemsSource类型?

[英]How do I specify a DataGrid's ItemsSource type in XAML?

I'm binding a DataGrid to an ICollectionView so that I can filter the ItemsSource efficiently, but ICollectionView is not a generic type (as in CollectionView<MyType> ) - it is of type List<object> . 我将DataGrid绑定到ICollectionView以便可以有效地过滤ItemsSource,但是ICollectionView不是泛型类型(如CollectionView<MyType> )-类型为List<object> So in the XAML editor, VisualStudio can't determine what the type is, so I don't get any IntelliSense help binding to the properties of the object in the collection view. 因此,在XAML编辑器中,VisualStudio无法确定类型是什么,因此我没有任何IntelliSense帮助绑定到集合视图中对象的属性。 It still builds and runs, but I don't get the help at design time. 它仍然可以构建和运行,但是在设计时我没有得到帮助。

Rephrasing the question: Is there anyway to "cast" the data-binding in XAML? 换个问题:换句话说,是否有“广播” XAML中的数据绑定?

I thought I could do something with <DataGrid.DataContext> , but I can't remember what it was and I haven't had any luck googling for it either: 我以为我可以使用<DataGrid.DataContext>做些什么,但我不记得它是什么,也没有运气来搜索一下:

XAML: 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>

Code Behind: 背后的代码:

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; }

    // ...
}

I thought I could do something with , but I can't remember what it was and I haven't had any luck googling for it either: 我以为可以做些什么,但我不记得它是什么,也没有运气来找它:

I believe the setting the design time data context is that you are looking for. 我相信设置设计时间数据上下文是您要寻找的。 Please refer to the following links for more information about this. 请参考以下链接以获取更多信息。

XAML: Intellisense for Bindings And the Data Context: 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:绑定和数据上下文的智能感知: https : //blogs.msmvps.com/deborahk/xaml-intellisense-for-bindings-and-the-data-context/ 如何在XAML编辑器中查看设计时数据绑定(它在运行时有效)?

What I'm trying to ask is if there is any way to "cast" the data-binding as a collection of MyType in XAML? 我想问的是,是否有任何方法可以将数据绑定“投射”为XAML中的MyType集合?

No. But you could specify a design-time DataContext as described above. 否。但是您可以如上所述指定设计时DataContext。

VisualStudio can't determine what the type is VisualStudio无法确定类型是什么

Since the type is not expressed until runtime and gotten by using reflection by the code, the designer is at a disadvantage and can only extrapolate on what it knows. 由于类型直到运行时才表达出来,并通过代码的反射来获取,因此设计者处于不利地位,只能对其了解的内容进行推断。 And all it knows is that it is an object but not the exact type which the developer knows. 它所知道的只是它是一个object而不是开发人员知道的确切类型。

I don't get any IntelliSense help ... still builds and runs, but I don't get the help at design time 我没有得到任何IntelliSense帮助...仍在构建和运行,但在设计时却没有得到帮助

If that is the issue which is holding you back, I recommend that you temporarily add a list of the property type in question to the VM (or page if not MVVM) and then bind to that new property. 如果那是使您退缩的问题,建议您将有问题的属性类型的列表临时添加到VM(如果不是MVVM,则添加页面),然后绑定到该新属性。 Then the design time will see what it needs and you can get property info add bindings/styles with the help of visual studio. 然后,设计的时候会看到它需要什么,你可以得到属性信息添加绑定/风格与Visual Studio的帮助。

Once all is good, replace the design time binding, as it were, to the ICollection you mentioned. 一旦一切顺利,就将设计时绑定替换为您提到的ICollection

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在XAML中设置DataGrid的ItemsSource? - How do I set the ItemsSource of a DataGrid in XAML? 如何使用XAML将DatagridComboBoxColumn绑定到不在DataGrid的ItemsSource中的ViewModel属性? - How do I use XAML to bind a DatagridComboBoxColumn to a ViewModel property that isn't in the DataGrid's ItemsSource? 如何在 UWP 项目中将 comboBox 绑定到与其父 DataGrid 的 ItemSource 不同的 ItemsSource? - How do I bind a comboBox to a different ItemsSource than it's parent DataGrid's ItemSource in a UWP project? 如何在VB代码后面初始化WPF datagrid ItemsSource? - How do I initialize a WPF datagrid ItemsSource in VB code behind? 如何在XAML中为自定义DataGrid控件指定GroupDescriptions? - How to specify GroupDescriptions in XAML for a custom DataGrid control? 如何指定单元格模板中使用的 XAML 仅在工具提示出现时执行? - How do I specify the XAML that's used in a cell template to only execute when the tooltip appears? 我如何使用XAML删除我的数据网格一侧的白色东西 - How do i remove this white thing on the side of my datagrid with XAML 如何让 XAML DataGridColumns 填充整个 DataGrid? - How do I make XAML DataGridColumns fill the entire DataGrid? 创建一个自定义DataGrid的ItemsSource - Create a custom DataGrid's ItemsSource 如何在我的datagrid XAML中将列标题居中 - How do I center the column header in my datagrid XAML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM