I want to be able to style my grid like this:
<DataGrid Style="{DynamicResource BilagDatagridStyle}">
<DataGrid.Columns>
</DataGrid.Columns>
</DataGrid>
But I can't figure out the correct syntax for setting the datagridcell style, I am getting an error on Property="DataGridCell.Style" complaning that it's the same setter as Property="DataGridRow.Style" the error is: Duplicate property 'Style' setter
<Style x:Key="DefaultReadOnlyGridStyle" TargetType="{x:Type DataGrid}" BasedOn="{StaticResource {x:Type DataGrid}}">
<Setter Property="CanUserAddRows" Value="False"/>
<Setter Property="CanUserDeleteRows" Value="False"/>
<Setter Property="CanUserSortColumns" Value="False"/>
<Setter Property="AutoGenerateColumns" Value="False"/>
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="IsReadOnly" Value="True"/>
<Setter Property="SelectionMode" Value="Single"/>
<Setter Property="HeadersVisibility" Value="Column"/>
<Setter Property="Width" Value="Auto"/>
<Setter Property="RowHeight" Value="30"/>
</Style>
<Style x:Key="BilagDatagridStyle" BasedOn="{StaticResource DefaultReadOnlyGridStyle}">
<Setter Property="DataGridCell.Style" Value="{DynamicResource DataGridRowNoInteractionStyle}"/>
<Setter Property="DataGridRow.Style" Value="{DynamicResource DataGridRowNoInteractionStyle}"/>
</Style>
I know that there is a way to do it, but how?
When you specify TargetType there is no need to add UIelement's name in property name. In your case appropiate is CellStyle and RowStyle, but if you get rid of TargetType then you have to use DataGrid.CellStyle/DataGrid.RowStyle.
<Style x:Key="YourCellStyle" TargetType="DataGridCell">
</Style>
<Style x:Key="YourRowStyle" TargetType="DataGridRow">
</Style>
<Setter Property="CellStyle" Value="{StaticResource YourCellStyle}"/>
<Setter Property="RowStyle" Value="{StaticResource YourRowStyle}"/>
Related
I'm trying to add a tooltip to textbox to show it when the text input is empty in TextChangedEvent,
I have tried this solution from this post How add and show tooltip textbox WPF if textbox not empty
<Style x:Key="TextBoxStyle" TargetType="TextBox">
<Setter Property="Padding" Value="5"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Width" Value="200"/>
<Style.Triggers>
<Trigger Property="ToolTip" Value="{x:Static sys:String.Empty}">
<Setter Property="ToolTipService.IsEnabled" Value="False" />
</Trigger>
</Style.Triggers>
</Style>
But I got this error :
Error A 'Binding' cannot be set on the 'Value' property of type 'Trigger'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.
How can I fix this problem ?
Update:
In addition, I would like to implement something like that ( without MVVM pattern ):
Source: Facebook Website
I didn't quite understand what kind of logic you want to implement.
From my guess, this is to show the ToolTip only when the line is empty or, on the contrary, do not show it for empty.
Here are both fully working options.
<Style x:Key="TextBoxStyle" TargetType="TextBox">
<Setter Property="Padding" Value="5"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Width" Value="200"/>
<Setter Property="ToolTipService.IsEnabled" Value="False"/>
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip>
<TextBlock Text="Hello World!"/>
</ToolTip>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Text" Value="{x:Static sys:String.Empty}">
<Setter Property="ToolTipService.IsEnabled" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="TextBoxStyle" TargetType="TextBox">
<Setter Property="Padding" Value="5"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="Width" Value="200"/>
<Setter Property="ToolTipService.IsEnabled" Value="True"/>
<Setter Property="ToolTip">
<Setter.Value>
<ToolTip>
<TextBlock Text="Hello World!"/>
</ToolTip>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="Text" Value="{x:Static sys:String.Empty}">
<Setter Property="ToolTipService.IsEnabled" Value="False" />
</Trigger>
</Style.Triggers>
</Style>
Using:
<TextBox Style="{DynamicResource TextBoxStyle}"/>
I want to show a red border around a Textbox when a validation error occurs. This works, but there is also a blue border showing inside the red one, which I don't want to be shown. Is there a way to remove this?
Style for the Textbox
<Style x:Key="StandardTextbox" TargetType="{x:Type TextBox}">
<Setter Property="Height" Value="20"/>
<Setter Property="Margin" Value="10,5,10,5" />
<Setter Property="FontSize" Value="12"/>
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="BorderBrush" Value="{StaticResource Blau}"/>
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
Usage in Window:
<TextBox Grid.Row="0" Grid.Column="1"
Text="{Binding Path=Location,
UpdateSourceTrigger=PropertyChanged,
ValidatesOnDataErrors=True,
NotifyOnValidationError=true}"
Style="{StaticResource StandardTextbox}" Grid.ColumnSpan="3"/>
Enlargement:
That's because you're setting BorderBrush to Blau inside your Style. You can remove it if there is any Validation error by using Triggers. Like,
<Style x:Key="StandardTextbox" TargetType="{x:Type TextBox}">
<Setter Property="Height" Value="20"/>
<Setter Property="Margin" Value="10,5,10,5" />
<Setter Property="FontSize" Value="12"/>
<Setter Property="VerticalAlignment" Value="Center" />
<Setter Property="BorderBrush" Value="{StaticResource Blau}"/>
<Setter Property="VerticalContentAlignment" Value="Center" />
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="BorderBrush" Value="Transparent"/>
</Trigger>
</Style.Triggers>
</Style>
I want to display datevalues in a DataGrid, whereas the text is vertically aligned in the center of the row, and not at the top. If I change the VerticalAlignment property in the DataGridCell Style, the text is aligned correctly, but a frame appears. Setting BorderThickness to 0 does not fix this.
How can I get rid of the frame/border and also display the text in the middle of the row?
DataGrid Style
<Style x:Key="StandardTabelle" TargetType="{x:Type DataGrid}">
<Setter Property="Margin" Value="5"/>
<Setter Property="Background" Value="White"/>
<Setter Property="AlternatingRowBackground" Value="#ebecec"/>
<Setter Property="FontSize" Value="12" />
<Setter Property="RowHeight" Value="24"/>
<Setter Property="ColumnHeaderStyle" Value="{StaticResource StandardSpaltenKopf}" />
<Setter Property="CanUserAddRows" Value="False"/>
<Setter Property="CanUserDeleteRows" Value="False"/>
<Setter Property="CanUserReorderColumns" Value="False"/>
<Setter Property="CanUserResizeRows" Value="False"/>
<Setter Property="CanUserResizeColumns" Value="False"/>
<Setter Property="IsReadOnly" Value="True" />
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="RowStyle" Value="{StaticResource AusgewählteZeile}"/>
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
Style for selected Row
<Style x:Key="AusgewählteZeile" TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource Rot}"/>
</Trigger>
</Style.Triggers>
</Style>
Style for Datecell with top alignment
<Style x:Key="DatumZelle" TargetType="DataGridCell">
<Setter Property="HorizontalContentAlignment" Value="Right"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="BorderThickness" Value="0"/>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Transparent" />
</Trigger>
</Style.Triggers>
</Style>
You could create a custom ControlTemplate:
<Style x:Key="DatumZelle" TargetType="DataGridCell">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Right" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="Transparent" />
</Trigger>
</Style.Triggers>
</Style>
I'm trying to create a simple TextBox style. I want to create a trigger to change the colors when the textbox is disabled:
<Style TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="White"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="FontSize" Value="{StaticResource NormalFontSize}"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="FontStyle" Value="Normal"/>
<Setter Property="FontFamily" Value="{StaticResource FontFamilyName}"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="VerticalAlignment" Value="Center"/>
<Setter Property="TextAlignment" Value="Center"/>
<Setter Property="HorizontalContentAlignment" Value="Left"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Background" Value="{StaticResource listItemHighlightBackground}"/>
<Setter Property="Foreground" Value="{StaticResource disabledArrowBackground}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
When I use the trigger, the textbox disappears
What's wrong here?
Thanks
A ControlTemplate replaces the template for that control type. It looks like you're misunderstanding its usage. Before the triggers, try adding some visual objects to display, such as a border, and that will appear. Better yet, don't use a ControlTemplate at all...you only need to set a property trigger on the style, e.g:
<Style.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property = "Foreground" Value="Green"/>
</Trigger>
</Style.Triggers>
I have a label which shows the name of the window. I want to update the colour of the label on the IsActive property of the window using styles and triggers so that all the labels inheriting this style should exhibit the same property. Please can anyone suggest me how?
I tried like this:
<Style TargetType="{x:Type Label}" x:Key="HeaderLabel">
<Style.Triggers>
<DataTrigger Binding="{Binding (Window.IsActive)}" Value="True">
<Setter Property="FontSize" Value="15"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
</DataTrigger>
<DataTrigger Binding="{Binding (Window.IsActive)}" Value="False">
<Setter Property="FontSize" Value="15"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
</DataTrigger>
</Style.Triggers>
</Style>
Try this binding in DataTrigger:
Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=IsActive}"
By label which shows the name of the window you mean the one on the window titlebar? Or something else?
If it's the latter, then you could set default style of the label and use only one trigger for inactive state. Also make sure you have a Window in label's datacontext. It should go like this (didn't check it):
<Style TargetType="{x:Type Label}" x:Key="HeaderLabel">
<Setter Property="FontSize" Value="15"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
<Style.Triggers>
<DataTrigger Binding="{Binding (Window.IsActive)}" Value="False">
<Setter Property="FontSize" Value="15"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="FontFamily" Value="Arial"/>
<Setter Property="Foreground" Value="White"/>
<Setter Property="HorizontalAlignment" Value="Left"/>
</DataTrigger>
</Style.Triggers>
</Style>
If you want to change titlebar, I think the easiest way would be to override Window style completely (for all windows themes).