<DataGridTextColumn Binding="{Binding Name}" Width="*"/> <DataGridTextColumn Binding="{Binding Change}" Width="Auto"/>
When the value of Change
updates, its column doesn't update to fit the new value. So the column stays too small and the value is clipped.
Any ideas?
The DataGrid will increase column sizes to fit as the data becomes longer, but it does not automatically decrease column sizes when the length of the data decreases. In your example, you're right aligning the 'Change' column, and using the rest of the space for the 'Name' column.
Now, when a 'Change' property grows large enough that it should increase the column's width, the 'Name' column is refusing to shrink to accommodate, so you have to force a refresh yourself.
1) In your DataGridTextColumn Bindings (all except your * sized column) set NotifyTargetUpdated=True.
2) On your DataGrid, add a handler to the TargetUpdated event.
3) In your TargetUpdated event handler:
-- a) Set the DataGrid's * sized column's width to 0.
-- b) Call the UpdateLayout() method on the DataGrid.
-- c) Set the DataGrid's * sized column's width back to new DataGridLength(1, DataGridLengthUnitType.Star)
<Window x:Class="DataGridTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <CollectionViewSource x:Key="MyObjectCollection" /> </Window.Resources> <DockPanel> <Button DockPanel.Dock="Bottom" Content="Click to Make Item 1s Text Longer" Click="Button_Click" /> <Grid> <DataGrid x:Name="dg" ItemsSource="{Binding Source={StaticResource MyObjectCollection}}" AutoGenerateColumns="False" TargetUpdated="dg_TargetUpdated"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding First}" Width="1*"/> <DataGridTextColumn Binding="{Binding Last, NotifyOnTargetUpdated=True}" Width="Auto" /> </DataGrid.Columns> </DataGrid> </Grid> </DockPanel> </Window>
using System; using System.Collections.ObjectModel; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.ComponentModel; namespace DataGridTest { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { private ObservableCollection<MyObject> myObjectList = new ObservableCollection<MyObject>(); public MainWindow() { InitializeComponent(); (this.FindResource("MyObjectCollection") as CollectionViewSource).Source = this.myObjectList; this.myObjectList.Add(new MyObject() { First = "Bob", Last = "Jones" }); this.myObjectList.Add(new MyObject() { First = "Jane", Last = "Doe" }); } private void Button_Click(object sender, RoutedEventArgs e) { this.myObjectList[0].Last = "BillyOBrian"; } private void dg_TargetUpdated(object sender, DataTransferEventArgs e) { dg.Columns[0].Width = 0; dg.UpdateLayout(); dg.Columns[0].Width = new DataGridLength(1, DataGridLengthUnitType.Star); } } public class MyObject : INotifyPropertyChanged { private string firstName; public string First { get { return this.firstName; } set { if (this.firstName != value) { this.firstName = value; NotifyPropertyChanged("First"); } } } private string lastName; public string Last { get { return this.lastName; } set { if (this.lastName != value) { this.lastName = value; NotifyPropertyChanged("Last"); } } } public MyObject() { } #region -- INotifyPropertyChanged Contract -- public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } #endregion INotifyPropertyChanged Contract } }
i have had similar problem with my listview, the solution i found on how-to-autosize-and-right-align-gridviewcolumn-data-in-wpf here on stackoverflow.
In my case it was adding this piece of code into collectionchanged event handler of the observable collection the list view was bound to:
void listview_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) { // this is a listview control GridView view = this.View as GridView; foreach(GridViewColumn c in view.Columns) { if(double.IsNaN(c.Width)) { c.Width = c.ActualWidth; } c.Width = double.NaN; } }
It works for me, although sometimes the user can notice "blink" over the columns.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With