DataGrid with multiple column collection, Select collection to show?

Jun 9, 2009 at 6:13 AM

Hi Everyone,

Could someone please show me how to change(select) column collections attached to the Column property?

Usually, I do this for normal column display.

 <my:DataGrid.Columns>
   <my:DataGridTemplateColumn Header="User ID" CellTemplate="{StaticResource key_DTLVC1}" IsReadOnly="True"/>
   <my:DataGridTemplateColumn Header="User Name" CellTemplate="{StaticResource key_DTLVC2}"/>
   <my:DataGridTemplateColumn Header="User Password" CellTemplate="{StaticResource key_DTLVC3}"/>
   <my:DataGridTemplateColumn Header="User Type" CellTemplate="{StaticResource key_DTLVC4}"/>
</my:DataGrid.Columns>

 But i want to delare multiple column collections in the XAML file under resource category and select the one to display. So I tried this one, where each style has a key and use a code behind file C# to assign the DataGrid.Style using FindResource("key_Style1").

 <font size="2" color="#a31515"><font size="2" color="#a31515">

 

</font></font><font size="2" color="#a31515">

 

</font>

<Style x:Key="key_StyleUsers" TargetType="{x:Type my:DataGrid}" x:key="key_Style1">
<Setter Property="Columns">
<Setter.Value>
<my:DataGridTemplateColumn Header="User ID" CellTemplate="{StaticResource key_DTLVC1}" IsReadOnly="True"/>
<my:DataGridTemplateColumn Header="User Name" CellTemplate="{StaticResource key_DTLVC2}"/>
<my:DataGridTemplateColumn Header="User Password" CellTemplate="{StaticResource key_DTLVC3}"/>
<my:DataGridTemplateColumn Header="User Type" CellTemplate="{StaticResource key_DTLVC4}"/>
</Setter.Value>
</Setter>
</Style>

 But results in error. It says only one object can be set under setter.value.  Is this a good approach?

Any advice guys?

 

 

<font size="2" color="#a31515"><font size="2" color="#a31515">

 

</font></font><font size="2" color="#a31515">

 

</font>

 

 

 

Coordinator
Jun 9, 2009 at 9:54 PM

Unfortunately I don't think we support this sort of operation from the XAML.  To get rid of the exception you're seeing, you could put the columns in the Setter inside some sort of collection, but this still wouldn't solve your problem because the Columns property is readonly, so you can't just set a new column collection inside it.  Instead, what you could do is define these different sets of columns in a ResourceDictionary, and then when you want to change the collection, in the code behind you need to call DataGrid.Columns.Clear() to clear the collection and then you can use DataGrid.Columns.Add() to individually add each column from your ResourceDictionary.

Thanks!
Samantha

Jun 10, 2009 at 2:28 AM

Hi, thanks for the reply. How to add the columns from the resource dictionary? There is no way I can define a collection of columns in XAML, then how can I define them is the resource dictionary.

In the ResourceDictionary.xaml, I cannot do the below code, cannot define a key
<font size="2" color="#ff0000"><font size="2" color="#ff0000">

xmlns

</font></font><font size="2" color="#ff0000">

 

</font>

:my=http://schemas.microsoft.com/wpf/2008/toolkit>

 

<font size="2" color="#a31515"><font size="2" color="#a31515">

 

</font></font><font size="2" color="#a31515">

 

</font>

 

<my:DataGrid.Columns>

 

<font size="2" color="#a31515"><font size="2" color="#a31515">

 

</font></font><font size="2" color="#a31515">

 

</font>

 

</my:DataGrid.Columns>

An idea would help.

Thanks!