我有一个WPF工具包DataGrid
,我想能够几个预置柱订单之间进行切换来控制列的顺序。这是一个MVVM项目,所以列订单存储在ViewModel
中。问题是,我无法获得绑定为DisplayIndex
属性工作。不管我怎么努力,包括甜方法this Josh Smith tutorial,我得到:
的的DisplayIndex与头“ID”的DataGridColumn超出范围。 DisplayIndex必须大于或等于0且小于Columns.Count。参数名称:displayIndex。实际值为-1。
有没有什么解决方法呢?
我在下面列出了我的测试代码。如果您发现任何问题,请告诉我。
视图模型代码
public class MainViewModel
{
public List<Plan> Plans { get; set; }
public int IdDisplayIndex { get; set; }
public int NameDisplayIndex { get; set; }
public int DescriptionDisplayIndex { get; set; }
public MainViewModel()
{
Initialize();
}
private void Initialize()
{
IdDisplayIndex = 1;
NameDisplayIndex = 2;
DescriptionDisplayIndex = 0;
Plans = new List<Plan>
{
new Plan { Id = 1, Name = "Primary", Description = "Likely to work." },
new Plan { Id = 2, Name = "Plan B", Description = "Backup plan." },
new Plan { Id = 3, Name = "Plan C", Description = "Last resort." }
};
}
}
计划类
public class Plan
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
窗口代码 - 这款采用Josh Smith's DataContextSpy
<Window
x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
xmlns:mwc="http://schemas.microsoft.com/wpf/2008/toolkit"
Title="Main Window" Height="300" Width="300">
<Grid>
<mwc:DataGrid ItemsSource="{Binding Plans}" AutoGenerateColumns="False">
<mwc:DataGrid.Resources>
<local:DataContextSpy x:Key="spy" />
</mwc:DataGrid.Resources>
<mwc:DataGrid.Columns>
<mwc:DataGridTextColumn
Header="ID"
Binding="{Binding Id}"
DisplayIndex="{Binding Source={StaticResource spy}, Path=DataContext.IdDisplayIndex}" />
<mwc:DataGridTextColumn
Header="Name"
Binding="{Binding Name}"
DisplayIndex="{Binding Source={StaticResource spy}, Path=DataContext.NameDisplayIndex}" />
<mwc:DataGridTextColumn
Header="Description"
Binding="{Binding Description}"
DisplayIndex="{Binding Source={StaticResource spy}, Path=DataContext.DescriptionDisplayIndex}" />
</mwc:DataGrid.Columns>
</mwc:DataGrid>
</Grid>
</Window>
注:如果我只是使用普通的号码DisplayIndex
,一切工作正常,那么问题肯定是绑定。
更新2010年5月1日
我只是在做我的项目很少的维护,我注意到,当我跑了,我在这个帖子讨论这个问题回来了。我知道它在我上次运行它的时候起作用,所以我最终将问题缩小到了我已经安装了WPF Toolkit的新版本(10年2月)。当我回到2009年6月份的版本时,一切正常。所以,我现在正在做一些我应该首先完成的工作:我将WPFToolkit.dll包含在我的解决方案文件夹中,并将其检入版本控制。但不幸的是,新的工具包有一个突破性的变化。
嗯,这很有趣。如果我运行我的测试应用程序,您的修复程序完美运行在我的真实应用程序中,所讨论的DataGrid实际上是在一个UserControl中,并且由于某种原因,如果我应用了'DataContextSpy'(我之前没有尝试过,因为它没有通过测试应用程序) ,即使我不推迟调用InitializeComponent(),它也可以工作。我认为这一定是因为我只是在ContentControl上设置Content,而不是做'window.Show()'。无论如何,感谢您花时间为我解决这个问题(+1和√)。 – devuxer 2010-01-24 21:41:23