DataGrid displays lengths of strings instead of the actual strings
-
Colleagues, Familiarizing myself with DataGrid (and with C# in general). My DataGrid is declared like this:
<my:DataGrid AutoGenerateColumns="True" Margin="74,32,68,103" Name="dataGrid1" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" />
I’m setting the ItemSource property equal to a list of strings.
public Window1()
{
InitializeComponent();List<string> lstGridData = new List<string>(); for (int i = 0; i < 8; ++i) lstGridData.Add("str"); dataGrid1.ItemsSource = lstGridData;
}
I’m expecting that grid would display one column and each line would show “str”. Instead, the grid displays one column titled “Length” and each line displays “3”. What am I missing? Cheers, - Nick
-
Colleagues, Familiarizing myself with DataGrid (and with C# in general). My DataGrid is declared like this:
<my:DataGrid AutoGenerateColumns="True" Margin="74,32,68,103" Name="dataGrid1" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" />
I’m setting the ItemSource property equal to a list of strings.
public Window1()
{
InitializeComponent();List<string> lstGridData = new List<string>(); for (int i = 0; i < 8; ++i) lstGridData.Add("str"); dataGrid1.ItemsSource = lstGridData;
}
I’m expecting that grid would display one column and each line would show “str”. Instead, the grid displays one column titled “Length” and each line displays “3”. What am I missing? Cheers, - Nick
The AutoGenerateColumns mechanism binds to any bindable properties it finds in the objects in the List. For a string, that's String.Length.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
The AutoGenerateColumns mechanism binds to any bindable properties it finds in the objects in the List. For a string, that's String.Length.
Mark Salsbery Microsoft MVP - Visual C++ :java:
How can I make a "bindable string"? Is there a framework object with a bindable string property which is commonly used for this purpose? May be, one option is to make a wrapper object with a bindable string property (call it, say, BindableString). Is there a simple/quick solution to "populate DataGrid with primitive values" problem? Tried with manually defined columns (AutoGenerateColumns=False). Didn't work either. Looking for a good DataGrid tutorial... Cheers, - Nick
modified on Monday, October 19, 2009 2:26 AM
-
How can I make a "bindable string"? Is there a framework object with a bindable string property which is commonly used for this purpose? May be, one option is to make a wrapper object with a bindable string property (call it, say, BindableString). Is there a simple/quick solution to "populate DataGrid with primitive values" problem? Tried with manually defined columns (AutoGenerateColumns=False). Didn't work either. Looking for a good DataGrid tutorial... Cheers, - Nick
modified on Monday, October 19, 2009 2:26 AM
Nick Alexeev wrote:
Is there a simple/quick solution to "populate DataGrid with primitive values" problem?
Manually bind to the object itself...
<my:DataGrid AutoGenerateColumns="False" Name="dataGrid1" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" > <my:DataGrid.Columns > <my:DataGridTextColumn Header="String" Binding="{Binding Path=.}" /> </my:DataGrid.Columns> </my:DataGrid>
Mark Salsbery Microsoft MVP - Visual C++ :java: