Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing bindings to control column order in a DataGrid
    text
    copied!<p><strong>Problem</strong></p> <p>I have a WPF Toolkit <code>DataGrid</code>, and I'd like to be able to switch among several preset column orders. This is an MVVM project, so the column orders are stored in a <code>ViewModel</code>. The problem is, I can't get bindings to work for the <code>DisplayIndex</code> property. No matter what I try, including the sweet method in <a href="http://blogs.infragistics.com/blogs/josh_smith/archive/2008/06/26/data-binding-the-isvisible-property-of-contextualtabgroup.aspx" rel="noreferrer">this Josh Smith tutorial</a>, I get:</p> <blockquote> <p>The DisplayIndex for the DataGridColumn with Header 'ID' is out of range. DisplayIndex must be greater than or equal to 0 and less than Columns.Count. Parameter name: displayIndex. Actual value was -1.</p> </blockquote> <p><strong>Is there any workaround for this?</strong></p> <p>I'm including my test code below. Please let me know if you see any problems with it.</p> <hr> <p><strong>ViewModel code</strong></p> <pre><code>public class MainViewModel { public List&lt;Plan&gt; 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&lt;Plan&gt; { 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." } }; } } </code></pre> <hr> <p><strong>Plan Class</strong></p> <pre><code>public class Plan { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } } </code></pre> <hr> <p><strong>Window code</strong> - this uses <a href="http://blogs.infragistics.com/blogs/josh_smith/archive/2008/06/26/data-binding-the-isvisible-property-of-contextualtabgroup.aspx" rel="noreferrer">Josh Smith's DataContextSpy</a></p> <pre><code>&lt;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"&gt; &lt;Grid&gt; &lt;mwc:DataGrid ItemsSource="{Binding Plans}" AutoGenerateColumns="False"&gt; &lt;mwc:DataGrid.Resources&gt; &lt;local:DataContextSpy x:Key="spy" /&gt; &lt;/mwc:DataGrid.Resources&gt; &lt;mwc:DataGrid.Columns&gt; &lt;mwc:DataGridTextColumn Header="ID" Binding="{Binding Id}" DisplayIndex="{Binding Source={StaticResource spy}, Path=DataContext.IdDisplayIndex}" /&gt; &lt;mwc:DataGridTextColumn Header="Name" Binding="{Binding Name}" DisplayIndex="{Binding Source={StaticResource spy}, Path=DataContext.NameDisplayIndex}" /&gt; &lt;mwc:DataGridTextColumn Header="Description" Binding="{Binding Description}" DisplayIndex="{Binding Source={StaticResource spy}, Path=DataContext.DescriptionDisplayIndex}" /&gt; &lt;/mwc:DataGrid.Columns&gt; &lt;/mwc:DataGrid&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <hr> <p><strong>Note:</strong> If I just use plain numbers for <code>DisplayIndex</code>, everything works fine, so the problem is definitely with the bindings.</p> <hr> <p><strong>Update 5/1/2010</strong></p> <p>I was just doing a little maintenance on my project, and I noticed that when I ran it, the problem I discuss in this post had returned. I knew that it worked last time I ran it, so I eventually narrowed the problem down to the fact that I had installed a newer version of the WPF Toolkit (Feb '10). When I reverted to the June '09 version, everything worked fine again. So, I'm now doing something I should have done in the first place: I'm including the WPFToolkit.dll that works in my solution folder and checking it into version control. It's unfortunate, though, that the newer toolkit has a breaking change.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload