I have a bug when I set the width of a column to * (i.e. take up all remaining space). This works fine if I populate the DataContext before I view the grid (e.g. in the constructor of WPF page) but fails when I populate the DataContext after the grid has been created and laid out (e.g. User choose an item to display in the grid at runtime).
Here is the code need to recreate the problem.
Class or storing some data
Code behind of Window class
XAML for window
Hope you can help.
Regards
Justin
Here is the code need to recreate the problem.
public class DummyObject |
{ |
public DummyObject(string name, string notes) |
{ |
Name = name; |
Notes = notes; |
} |
public string Name |
{ |
get; |
set; |
} |
public string Notes |
{ |
get; |
set; |
} |
} |
public partial class Window1 : Window |
{ |
public Window1() |
{ |
InitializeComponent(); |
//This works fine |
//PopulateGrid(); |
} |
private void Populate_Click(object sender, RoutedEventArgs e) |
{ |
//This will throw an exception |
PopulateGrid(); |
} |
private void PopulateGrid() |
{ |
List<DummyObject> myObjects = new List<DummyObject>() |
{ |
new DummyObject("Justin", "Here is a note"), |
new DummyObject("Paul", "Paul has a note too"), |
new DummyObject("Sam", "Sam has a note that is much longer than the other notes"), |
new DummyObject("Dean", "Nothing to see here"), |
new DummyObject("Andrew", "Blah blah blah blah") |
}; |
mainGrid.DataContext = myObjects; |
} |
} |
<Window x:Class="TeleirkDatagridBug.Window1" |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" |
Title="Window1" Height="300" Width="300"> |
<DockPanel> |
<StackPanel DockPanel.Dock="Top"> |
<Button Content="Populate" Click="Populate_Click"/> |
</StackPanel> |
<telerik:RadGridView AutoGenerateColumns="False" x:Name="mainGrid" ItemsSource="{Binding}"> |
<telerik:RadGridView.Columns> |
<telerik:GridViewDataColumn Width="100" HeaderText="Name" UniqueName="Name"/> |
<telerik:GridViewDataColumn Width="*" HeaderText="Notes" UniqueName="Notes"/> |
</telerik:RadGridView.Columns> |
</telerik:RadGridView> |
</DockPanel> |
</Window> |
Hope you can help.
Regards
Justin