Search This Blog

Monday, May 23, 2016

DataGridRow change color of selected row

WPF > DataGrid Color of selected row


  <DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#FFB8AEAE"/>
  </DataGrid.Resources>


Last column width in WPF datagrid to take all the left space

WPF > DataGrid > Last column width


XAML

<DataGridTemplateColumn Header="Range" Width="100'*">

Or Code behind:

 // make the last column in WPF data grid take all the left space, always

dataGrid.Columns[dataGrid.Columns.Count - 1].Width = new DataGridLength(1, DataGridLengthUnitType.Star);


Friday, May 20, 2016

WPF datagrid edit cell on single click

WPF > DataGrid > Edit cell on single click

<Window.Resources>
        <Style TargetType="{x:Type DataGridCell}">
            <EventSetter Event="PreviewMouseLeftButtonDown"    Handler="DataGridCell_PreviewMouseLeftButtonDown" />
        </Style>
    </Window.Resources>

private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DataGridCell cell = sender as DataGridCell;
    EditCell(cell, e);
}

private void EditCell(DataGridCell cell, RoutedEventArgs e)
{
    if (cell == null || cell.IsEditing || cell.IsReadOnly)
       return;

    if (!cell.IsFocused)
    {
                cell.Focus();
    }
    DGrid.BeginEdit(e);
    cell.Dispatcher.Invoke(
           DispatcherPriority.Background,
           new Action(delegate { })
    );

}