【发布时间】: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
-
@OmegaMan 不,它仍在构建和运行。我的问题是如何在设计时指定
ItemsSource实际上是List<MyType>,而不是List<object>- 获得随之而来的所有智能感知和类型检查优势。我更新了我的问题以解决您的评论。 -
@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