How can I set the width of a DataGridColumn to fit contents (“Auto”), but completely fill the available space for the DataGrid in MVVM?

I would suggest using the workaround that you linked to. It does work. In the codebehind of your view, add the following to the constructor after InitializeComponent().

I would suggest using the workaround that you linked to. It does work. In the codebehind of your view, add the following to the constructor after InitializeComponent(): Griddy.

Loaded += SetMinWidths; // I named my datagrid Griddy and define SetMinWidths as: public void SetMinWidths(object source, EventArgs e ) { foreach (var column in Griddy. Columns) { column. MinWidth = column.

ActualWidth; column. Width = new DataGridLength(1, DataGridLengthUnitType. Star); } } I'm assuming that the reason you don't want to use this solution is that you believe that MVVM prohibits code in the codebehind.

But since this is entirely view specific logic I believe that it is justified in this case. The whole "MVVM prohibits code in the codebehind" thing is a bit of a misconception. But if you are bound by style guides, or you want this logic to be available for all datagrids in your app, you can make an attached behaviour that does the job like this: public class SetMinWidthToAutoAttachedBehaviour { public static bool GetSetMinWidthToAuto(DependencyObject obj) { return (bool)obj.

GetValue(SetMinWidthToAutoProperty); } public static void SetSetMinWidthToAuto(DependencyObject obj, bool value) { obj. SetValue(SetMinWidthToAutoProperty, value); } // Using a DependencyProperty as the backing store for SetMinWidthToAuto. This enables animation, styling, binding, etc... public static readonly DependencyProperty SetMinWidthToAutoProperty = DependencyProperty.

RegisterAttached("SetMinWidthToAuto", typeof(bool), typeof(SetMinWidthToAutoAttachedBehaviour), new UIPropertyMetadata(false, WireUpLoadedEvent)); public static void WireUpLoadedEvent(DependencyObject d, DependencyPropertyChangedEventArgs e) { var grid = (DataGrid)d; var doIt = (bool)e. NewValue; if (doIt) { grid. Loaded += SetMinWidths; } } public static void SetMinWidths(object source, EventArgs e) { var grid = (DataGrid)source; foreach (var column in grid.

Columns) { column. MinWidth = column. ActualWidth; column.

Width = new DataGridLength(1, DataGridLengthUnitType. Star); } } } and then for any datagrid that you want to apply this behaviour to, add the following: And then your conscience, as well as your codebehind, will be clear.

1 thanks a lot for that! Actually, I don't think that completely view specific stuff should not be in the code-behind. The problem is rather that the content of the DataGrid changes in the ViewModel, so the View does not know when it has to call SetMinWidths again.

Maybe there would be a way that the DataGrid changes as soon as my NotifyPropertyChanged method gets called? I think that is the same problem with the attached behaviour, but I will probably find a way to fix that. Will be back with more infos soon.

Thanks again! – Sören Feb 18 '11 at 9:20 see my post, I edited it: Unfortunately, I didn't get it to work like that. For the reasons see above.

– Sören Feb 18 '11 at 10:42.

Use a TextBlock with text wrapping inside template column.

Thanks for your suggestion, but I don't want to wrap the text. As mentioned above, I would rather like to get a vertical scrollbar as soon as the text gets too wide. – Sören Feb 14 '11 at 23:07 The point of templated columns is that you can use whatever control fits your needs inside the DataTemplate, for example – Rafa Castaneda Feb 14 '11 at 23:49 I don't want to have a scrollbar in the specific column, but rather one for the whole datagrid which also comes up if I set the width of the columns on "Auto" and the space does not suffice... it would look very bad if you have a scrollbar in every cell... – Sören Feb 15 '11 at 11:28.

If you want the column to never cut off your data, add the following to your Window/UserControl definition: xmlns:System="clr-namespace:System;assembly=mscorlib" Then for the width of the Columns you can use to following. Width="{x:Static System:Double. NaN}" This makes the column always grow wide enough to accommodate the longest value in the column.As the above code didn't work in the DataGrid, what about adding a ScrollViewer around the DataGrid like this.

Thanks for the answer. However, I am getting a XamlParseException after adding that. Also, I think this would end in showing the exact same behaviour as setting the Width to "Auto" (actually, I think "Auto" is internally represented as Double.

NaN). – Sören Feb 14 '11 at 21:51 I had used this on GridViews and it worked great for Dynamically sizing the columns. Tried it with the DataGrid this morning and got the same Xaml error.

– Wes Grant Feb 15 '11 at 13:40.

How about this: for your columns' MinWidth, you setup a binding to the dataGrid's ScrollContent's Width, with a converter that divide this width by the number of columns you need some code behind for the converter, but this would keep your MVVM structure intact. Might be a long shot though, did not have the time to try it. Edit: I put some more thought into this and there a pb: it will not work well if you have, say, one huge column and a few small ones.

I assumed that all cols are the same width, which is obviously not the case here. You might want to explore this way though. Using bindings on the Widths with converters is about the only thing I can think of that could work: since you basically have 2 conditions to take into account when calculating a column's width, there will be no easy way to do this.

Okay, thanks for the idea, I will think about that. However, I indeed have one columns that gets much wider than the other columns, so it won't work with the first suggestion. I think you are right that I need to use bindings for the width of the columns (that is what I think as well), but it will still be a challenge to find the right minwidth/width for each column ^^ – Sören Feb 17 '11 at 13:18 it certainly will.

^^ If I may add a piece of advise: don't use the MinWidth property in conjunction with the Width property if you can avoid it. It can lead to massive headache. E.g.

: set the minWidth to 100, then the width to 80, and the control's width will be 80: the width takes over the minWidth if you set it by program... – David Feb 17 '11 at 13:21.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions