Reputation: 328
There is a WPF window and on it a DataGrid. There is a column "Name", which can be edited and according to its size varies - it expands.
I'm interested in the reverse process - that it would narrow down when the user erases some characters. Here's how it is at the moment:
And here's how I need it to be after the symbols are erased:
I set through the Setter in the styles for the cells of the column values
<Setter Property="HorizontalContentAlignment" Value="Center" />
and
<Setter Property="HorizontalAlignment" Value="Center" />
but it did not help. I also tried to enter TextBlock for the cells of this column - also unsuccessfully. Width = Auto;
- is also not an option.
Tell me, please, how to make the column width vary depending on the content. Here is the xml project code:
<Grid x:Name="checkinGrid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,0,10,0" MinHeight="300" MinWidth="535">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="65"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<DataGrid x:Name="filesGrid" AutoGenerateColumns="False" HorizontalGridLinesBrush="DarkGray"
RowBackground="LightGray" AlternatingRowBackground="LightGray" Margin="40,25,45,10" Grid.Row="0" Grid.Column="0"
VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
CanUserAddRows ="false" CanUserDeleteRows ="false" ScrollViewer.HorizontalScrollBarVisibility="Visible">
<DataGrid.Columns>
<DataGridTextColumn Header="File" Binding="{Binding Path=File}" IsReadOnly="True">
<DataGridTextColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>
<DataGridTextColumn x:Name="nameDataGridTextColumn" Header="Name" Binding="{Binding Path=Name}">
<DataGridTextColumn.HeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Center" />
</Style>
</DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>
</DataGrid.Columns>
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#00FFFFFF"/>
</DataGrid.Resources>
</DataGrid>
[Edited]
I set event CellEditEnding="DataGrid_CellEditEnding"
for the dataGrid:
private void DataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
nameDataGridTextColumn.Width = -1;
}
Upvotes: 0
Views: 274
Reputation: 26095
Not sure what you are trying to achieve but calling CellEditEnding
event handler will be executed when you finish editing textbox and move focus out of it and not when you change content inside the textbox. You can fix your code by changing it to
private void arasFilesCheckInGrid_CellEditEnding(Object sender, DataGridCellEditEndingEventArgs e)
{
filesGrid.Columns[1].Width = new DataGridLength(0);
filesGrid.Columns[1].Width = new DataGridLength();
}
This will force grid to recalculate width of the column at index 1.
Upvotes: 1