datagridHeader is being smaller when the scrollbar is appearing to fit the data - wpf

enter image description hereI am using this datagrid, when the data is few the header is Nice, but when the data rows is many so the scrollbar is visible and the header row is shorter, so how can i let it not short even if the scrollbar is here? My Problem image
<DataGrid Margin="10"
HeadersVisibility="All"
FlowDirection="{StaticResource DirectFlowDirection}"
Foreground="{DynamicResource MainBrush}"
Background="{DynamicResource DataGridBackgroundBrush}"
RenderTransformOrigin="0.5,0.5">
<DataGrid.Resources>
<Style BasedOn="{StaticResource Rows}" TargetType="DataGridRow">
<EventSetter Event="MouseDoubleClick" Handler="datagrid_MouseDoubleClick"/>
<Setter Property="ContextMenu" Value="{StaticResource ContextMenu}"/>
<!--<Setter Property="HorizontalContentAlignment" Value="Center"></Setter>-->
</Style>
<Style TargetType="DataGridRowHeader">
<Setter Property="Width" Value="1*"></Setter>
</Style>
<Style BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="Background" Value="{DynamicResource DataGridHeaderColor}"/>
<Setter Property="Foreground" Value="{DynamicResource DataGridHeaderForeground}"/>
<Setter Property="HorizontalContentAlignment" Value="Center"></Setter>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>MyColumns here</DataGrid.Columns>
I want the header row keep in the entire window width

Re templating with the scroll viewer just round the itemspresenter looks possible, but you already have styling in your project.
The template is here:
https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/datagrid-styles-and-templates?view=netframeworkdesktop-4.8
If your concern is purely cosmetic you could consider putting a small blue rectangle on top of the top left corner to sort of fill in that area. Maybe make it hittestable false.

Related

WPF XAML TextBox not editable after applying Control Template

I have a TextBox in WPF and I'm trying to make the border color change when the mouse hovers over the TextBox. Based on my experience with other elements in WPF, I need to insert a ControlTemplate value with TemplateBinding to the values I am trying to dynamically change. However, when I apply this, the box becomes uneditable (and the text disappears). If I remove the Template setter, the box becomes editable again, but the custom BorderBrush triggers do not work.
Here is the Style:
<Style x:Key="TextBoxBase" TargetType="TextBox">
<Setter Property="FontSize" Value="30"/>
<Setter Property="Background" Value="{StaticResource BrushLightGrey}"/>
<Setter Property="Foreground" Value="{StaticResource BrushNormalText}"/>
<Setter Property="IsReadOnly" Value="False"/>
<Setter Property="Height" Value="40"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border BorderBrush="{TemplateBinding BorderBrush}">
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="{StaticResource BrushBlue}"/>
</Trigger>
</Style.Triggers>
<Style.Resources>
<Style TargetType="{x:Type Border}">
<Setter Property="CornerRadius" Value="5"/>
</Style>
</Style.Resources>
</Style>
Any suggestions or help is appreciated. Thanks.
You missed out the critical part:
<ScrollViewer Margin="0"
x:Name="PART_ContentHost" />
This is what hosts the text.
See
https://learn.microsoft.com/en-us/dotnet/framework/wpf/controls/textbox-styles-and-templates
TextBox Parts
The following table lists the named parts for the TextBox control.
TEXTBOX PARTS
Part Type Description
PART_ContentHost FrameworkElement A visual element that can contain a FrameworkElement. The text of the TextBox is displayed in this element.

Setting AlternatingRowBackground for DataGrid with Templated DataGridRow

I need to have DataGrid with black and dark gray rows. By default it would have gray rectangle in the left of each row (marked by red in screenshot) which I don't need. To remove it I have to use Template for DataGridRow. The problem I get is that AlternatingRowBackground wouldn't work in that case, though I tried to set Background="Transparent" for Border and DataGridCellsPresenter. I found example with VisualStates but this code seems very heavy. Is there neat way to fix it?
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridRow}">
<Border>
<DataGridCellsPresenter/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type DataGrid}">
<Setter Property="Background"
Value="Black" />
<Setter Property="Foreground"
Value="White" />
<Setter Property="RowBackground"
Value="Black" />
<Setter Property="AlternatingRowBackground"
Value="DarkGray" />
</Style>
The gray rectangle is the so called row Header. By default the DataGrid turns on row and column header. You turn it off the row header by setting
HeadersVisibility=Column
So, you don't need a template to get that done.

ControlTemplate.Trigger not working on CellValuePresenter (TargetType)

I 'm using Infragistics 2014.2. I have a XamDataGrid which display the data with dynamic columns, the grid is formatted by its default style and in addition, some of the columns having alignment set to Right (decimal type columns) with some value masking on it and the rest are set alignment to Left.
I want to remove the default CellValuePresenter style of XamDataGrid and write a new style with only TextBlock/ContentPresenter on it instead of CellValuePresenter for each cell and after applying new style, the alignment and masking of the cells should remain same. The reason behind applying this new style is i want the copy of the same grid to display it on another window with no data manipulation in it.
Can you please look at the following code that i have tried to achieve the same(even with many of the changes in it), but it doesn't seems to work anyhow.
<Style x:Key="PreviewCellValuePresenterStyle" TargetType="{x:Type igDP:CellValuePresenter}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate x:Name="CellValuePresenterControlTemplate" TargetType="{x:Type igDP:CellValuePresenter}">
<ContentPresenter x:Name="ContentValuePresenter"
VerticalAlignment="Center"
ContentSource="Value"
Margin="4,3,4,4"/>
<ControlTemplate.Triggers>
<Trigger Property="HorizontalAlignment" Value="Right">
<Setter TargetName="ContentValuePresenter" Property="TextBlock.TextAlignment" Value="Right"></Setter>
</Trigger>
<Trigger Property="HorizontalContentAlignment" Value="Right">
<Setter TargetName="ContentValuePresenter" Property="TextBlock.TextAlignment" Value="Right"></Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Right now, i'm only setting the alignment property for testing. In the above code, have tried TextBlock in place of ContentPresenter but the trigger never gets invoked. I don't where i'm doing wrong.
Can you anyone help me to get out of this?
Thanks
If your intention of using text block is only for alignment/appearance, then you can just use setters to set these values.
<Style x:Key="PreviewCellValuePresenterStyle" TargetType="{x:Type igDP:CellValuePresenter}" >
<Setter Property="FontWeight" Value="DemiBold"/>
<Setter Property="Height" Value="24"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Top"/>
</Style>
But, If you need some other implementations like value masking, or need text block to be there,
<Style x:Key="PreviewCellValuePresenterStyle" TargetType="{x:Type igDP:CellValuePresenter}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
<TextBlock HorizontalAlignment="Center" Style="{DynamicResource TextStyle}">
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="TextStyle" TargetType="{x:Type TextBlock}">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="HorizontalContentAlignment" Value="Center"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
</Style>

WPF Modifying scrollbar in DataGrid issue

I am trying to style the scrollbars in my datagrids using app.xaml (want it to be global across the app)
My code I have done is
<Style TargetType="{x:Type DataGrid}">
<Setter Property="ScrollViewer.Background" Value="LightBlue"></Setter>
<Setter Property="ScrollViewer.Foreground" Value="LightGray"></Setter>
</Style>
However, it doesn't work, is there anything I have missed?
Cheers
Try this
<Style TargetType="{x:Type DataGrid}">
<Style.Resources>
<Style TargetType="ScrollViewer">
<Setter Property="Background" Value="LightBlue"/>
<Setter Property="TextElement.Foreground" Value="LightGray"/>
</Style>
</Style.Resources>
</Style>
Note : for scrollbar change target type <Style TargetType="Scrollbar">

Coded UI Test in wpf datagrid doesn't recognize cells and rows when row style is applied

In WPF Datagrid, if row style is used, it does not detect the grid rows. If I remove the row style, it detects the cell and rows properly.
<DataGrid Grid.Row="1" x:Name="c_dataGrid"
ItemsSource="{Binding DataSource}"
AutoGenerateColumns="True"
RowStyle="{StaticResource rowStyle}">
<DataGrid.ItemContainerStyle>
<Style>
<Setter Property="AutomationProperties.Name" Value="{Binding Id}"/>
</Style>
</DataGrid.ItemContainerStyle>
</DataGrid>
<Style x:Key="rowStyle" TargetType="{x:Type DataGridRow}"
<Setter Property="Foreground" Value="Green"/>
</Style>
A shot in the dark here...
Does it make any difference if you apply the BasedOn property?
<Style
x:Key="rowStyle"
TargetType="{x:Type DataGridRow}"
BasedOn="{StaticResource {x:Type DataGridRow}}">
<Setter Property="Foreground" Value="Green"/>
</Style>

Resources