How to resize a datagrid column when datagrid resizes in wpf?
Would post this on SO, but I'll be honest I am scared of that community. Feel safer around.
Anyways, I have an MVVM WPF application that has a datagrid within a user control. That user control is within a tab. I would like the first column take up as much room as possible (Width="*") and the rest take whatever. Usually the star would take care of this problem. However, I am not sure what I am doing wrong, but doing so crops the entire column out as if the grid had no width. The solution I was using then was to give the column a MinWidth, but that just does not sit right with me; and the other issue being that if the grid or form changes size, none of the data grid columns resize. The user also cannot resize the columns either, though it displays the double arrowhead. I tried a lot of things: setting ColumnWidth="*" in the data grid tag, setting Width="auto", and then even resizing on the loaded/resize events from code behind. Nothing is working and it is really starting to become obnoxious.
Link of what I tried with code behind: https://stackoverflow.com/questions/4997596/how-can-i-set-the-width-of-a-datagridcolumn-to-fit-contents-auto-but-comple
this is what is in the user control:
<Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <DataGrid Grid.Row="0" AutoGenerateColumns="False" ItemsSource="{Bindingtems}" SelectionMode="Single" ColumnWidth="*" SelectedItem="{Binding SelectedLine}"> <DataGrid.Resources> <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{StaticResource SelectedDataGridItemBackgroundColor}"/> </DataGrid.Resources> <DataGrid.Columns> <DataGridTextColumn Header="Item Description" Width="*" Binding="{Binding Description, UpdateSourceTrigger=PropertyChanged}"/> <DataGridTextColumn Header="Quantity" MinWidth="100" Binding="{Binding Quantity, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> <DataGridTextColumn Header="Total" MinWidth="100" Binding="{Binding LineBillingTotal, UpdateSourceTrigger=PropertyChanged, StringFormat='{}{0:c}'}"/> </DataGrid.Columns> </DataGrid> </Grid>
Tab Item where the grid is:
<TabItem.ContentTemplate > <DataTemplate > <local:DetailView DataContext="{Binding Source={StaticResource Locator}, Path=Detail}" /> </DataTemplate> </TabItem.ContentTemplate>
0 comments:
Post a Comment