dev*_*xer 12 data-binding wpf xaml mvvm wpftoolkit
问题
我有一个WPF工具包DataGrid,我希望能够在几个预设的列顺序之间切换.这是一个MVVM项目,因此列顺序存储在一个ViewModel.问题是,我无法获得为该DisplayIndex属性工作的绑定.无论我尝试什么,包括这个Josh Smith教程中的甜蜜方法,我得到:
带标题"ID"的DataGridColumn的DisplayIndex超出范围.DisplayIndex必须大于或等于0且小于Columns.Count.参数名称:displayIndex.实际值为-1.
这有什么解决方法吗?
我在下面提供了我的测试代码.如果您发现任何问题,请告诉我.
ViewModel代码
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." }
};
}
}
Run Code Online (Sandbox Code Playgroud)
计划类
public class Plan
{
public int Id { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
窗口代码 - 这使用了Josh Smith的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>
Run Code Online (Sandbox Code Playgroud)
注意:如果我只使用普通数字DisplayIndex,一切正常,所以问题肯定是绑定.
2010年5月1日更新
我只是对我的项目做了一些维护,我注意到当我运行它时,我在这篇文章中讨论的问题又回来了.我知道上次运行时它运行起来了,所以我最终将问题缩小到我安装了WPF Toolkit的更新版本(2010年2月).当我回到2009年6月的版本时,一切都恢复正常.所以,我现在正在做一些我本应该做的事情:我包括在我的解决方案文件夹中工作的WPFToolkit.dll并将其检入版本控制.不幸的是,更新的工具包有一个突破性的变化.
Pak*_*man 12
FallbackValue=<idx>在DisplayIndex绑定中设置,<idx>列的索引在哪里.例如:
DisplayIndex="{Binding Source={StaticResource spy}, Path=DataContext.IdDisplayIndex, FallbackValue=0}" />
Run Code Online (Sandbox Code Playgroud)
(我之前的答案偏离了轨道 - 所以我删除了它 - 在我的机器上重现错误后我会尝试再次回答)
您遇到的问题源于这样一个事实:当第一次评估绑定时,该DataContext属性仍然设置为null。由于某种奇怪的原因,但我不知道,这会导致绑定在 处求值-1。但是,如果您确保在评估绑定DataContext 之前设置,那就没问题了。不幸的是,这可能只能在代码隐藏中实现 - 因此在运行时,而不是在设计时。所以在设计的时候我还是不知道如何规避这个错误。
为此,请DataContext在调用之前设置窗口的属性InitializeComponent:
public MainWindow()
{
DataContext = new MainViewModel();
InitializeComponent();
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助。
| 归档时间: |
|
| 查看次数: |
7733 次 |
| 最近记录: |