Why doesn't this simple Style work for a TextBox? I expect the background/foreground colors to change when I change the text between "0" and "1" ...
<Style x:Key="TextBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Gray"/>
<Style.Triggers>
<!-- If the Textbox holds a value of 1, then update the foreground/background -->
<DataTrigger Binding="{Binding Path=Text}" Value="1">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background" Value="White"/>
</DataTrigger>
<!-- If the Textbox holds a value of 0, then update the foreground/background -->
<DataTrigger Binding="{Binding Path=Text}" Value="0">
<Setter Property="Foreground" Value="White"/>
<Setter Property="Background" Value="Black"/>
</DataTrigger>
</Style.Triggers>
</Style>
You use a DataTrigger but better in this case would be a trigger:
<Trigger Property="Text" Value="1">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background" Value="White"/>
</Trigger>
Related
I managed to change the color of the highlighted column in the code behind. If the row is selected, it is necessary that the cell be in a different color. It would be nice if it could be done in a designer.
It should be a different color when selected
The following is an example:
Dim s As Style = New Style(GetType(DataGridCell))
s.Setters.Add(New Setter(BackgroundProperty, Brushes.LightBlue))
s.Setters.Add(New Setter(BorderThicknessProperty, New Thickness(0)))
s.Setters.Add(New Setter(ForegroundProperty, Brushes.Black))
s.Setters.Add(New Setter(TextBlock.TextAlignmentProperty, TextAlignment.Right))
DgwRacunStavke.Columns(DgwRacunStavke.IndexKoloneNaOsnovUNaziva("Može da se upiše u refundaciju")).CellStyle = s
I tried this in a designer but binding doesn't work because I probably made a mistake:
<DataGrid.CellStyle>
<Style TargetType="DataGridCell" >
<Style.Triggers>
<DataTrigger Binding="{Binding Header}" Value="Može da se upiše u refundaciju"/>
</Style.Triggers>
<Setter Property="Background" Value="{StaticResource bojaZaIsticanjeKoloneUGridu}"/>
<Setter Property="BorderBrush" Value="{StaticResource bojaZaIsticanjeKoloneUGridu}"/>
<Setter Property="Foreground" Value="{StaticResource bojaFontaZaIsticanjeKoloneUGridu}"/>
</Style>
</DataGrid.CellStyle>
Obviously I need a trigger for IsSelected ...
EDIT: Solution
XAML:
<Style x:Key="DataGridCell_Isticanje" TargetType="DataGridCell">
<Style.Triggers>
<Trigger Property="IsSelected" Value="False">
<Setter Property="Background" Value="{StaticResource bojaZaIsticanjeKoloneUGridu}"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Foreground" Value="{StaticResource bojaFontaZaIsticanjeKoloneUGridu}"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{StaticResource bojaMouseOver}"/>
<Setter Property="BorderThickness" Value="0"/>
<Setter Property="Foreground" Value="{StaticResource bojaFontaKadaJeSelektovan}"/>
</Trigger>
</Style.Triggers>
</Style>
CODE BEHIND:
DGW.Columns(DGW.IndexKoloneNaOsnovUNaziva("Može da se upiše u refundaciju")).CellStyle = FindResource("DataGridCell_Isticanje")
Have you tried to put the <Setter> before the </Style.Trigger> It should look something like this.
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=element2, Path=IsActive}" Value="False">
<Setter Property="Foreground" Value="#787878" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=element1, Path=IsSelected}" Value="True">
<Setter Property="Foreground" Value="Aquamarine"/>
<Setter Property="FontWeight" Value="Bold"/>
</DataTrigger>
</Style.Triggers>
i would like to change the background of a ListBox to red if the SelectedIndex of ListBox greater than -1. but in xaml i only can use "=".
<ListBox.Style>
<Style TargetType="ListBox">
<Style.Triggers>
<Trigger Property="SelectedIndex" Value="-1">
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Style>
What i want is:
<ListBox.Style>
<Style TargetType="ListBox">
<Style.Triggers>
<Trigger Property="SelectedIndex" Value>"-1">
<Setter Property="Background" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Style>
Is it possible to do this in xaml?
Do it the other way round:
<ListBox.Style>
<Style TargetType="ListBox">
<Setter Property="Background" Value="Red"/>
<Style.Triggers>
<Trigger Property="SelectedIndex" Value="-1">
<Setter Property="Background" Value="Transparent"/>
</Trigger>
</Style.Triggers>
</Style>
</ListBox.Style>
I have run into a few issues with gridcontrol.
I have to style and format a grid column with padding,colors,fonts and hover effects.
<Style x:Key="SelectedRowStyle" TargetType="{x:Type dxg:RowControl}">
<Setter Property="Foreground" Value="Black" />
<Setter Property="FontFamily" Value="pack://application:,,,/PA.Tos.UI;component/ResourceDictionaries/#Brandon Grotesque Black" />
<Setter Property="FontSize" Value="12" />
<Setter Property="FontWeight" Value="Regular" />
<Style.Triggers>
<DataTrigger Binding="{Binding
ElementName=GroupCodeListView,Path=DataContext.SelectedGroupCode.Deleted,
UpdateSourceTrigger=PropertyChanged}" Value="true">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Black" />
</DataTrigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource HoverRowBorderColor}" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
<Trigger Property="dxg:GridViewBase.IsFocusedRow" Value="True">
<Setter Property="Background" Value="{StaticResource HoverRowBorderColor}" />
<Setter Property="BorderBrush" Value="{StaticResource HoverStrokeColor}" />
<Setter Property="BorderThickness" Value="1" />
<Setter Property="Foreground" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
<Style x:Key="CustomCellStyle" BasedOn="{StaticResource {dxgt:GridRowThemeKey
ResourceKey=LightweightCellStyle}}" TargetType="{x:Type dxg:LightweightCellEditor}">
<Setter Property="MaxHeight" Value="25"/>
<Setter Property="MinHeight" Value="25"/>
<Style.Triggers>
</Style.Triggers>
In response to a mouse hover or row selection, i have to set the border blue across all grid lines. Only the bottom grid line is blue as of
now from the above. Code applicable to cellcontent presenter won't be possible here.
In response to a trash icon clicked, i have to display light red background for the particular row.
I bind (viewmodel property)SelectedGroupCode.Deleted=true to the background.The binding is shown in the code.
but all rows are painted red except the row in question.
The grid lines width has to be set. i have managed to set it for the horizontal lines only using gridrowthemekey_rowcontrolcontainertemplate.
I assure you i have read through some previous threads but its taking too much time for a scrum sprint.
What is missing?
If you want to change the cell style in response to a mouse hover then you can use RelativeSource markup extension in DataTrigger's binding. If you want to check whether the row is focused, then you can use RowData.IsFocused property.
Here is example:
<Style x:Key="CustomCellStyle" TargetType="{x:Type dxg:LightweightCellEditor}" BasedOn="{StaticResource {dxgt:GridRowThemeKey ResourceKey=LightweightCellStyle}}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsMouseOver, RelativeSource={RelativeSource AncestorType={x:Type dxg:RowControl}}}" Value="True">
<Setter Property="BorderBrush" Value="Blue" />
</DataTrigger>
<DataTrigger Binding="{Binding RowData.IsFocused}" Value="true">
<Setter Property="BorderBrush" Value="Blue" />
</DataTrigger>
</Style.Triggers>
</Style>
For displaying custom style for particular row I suggest you to use Conditional Formatting.
Here is example:
<dxg:GridControl ...>
...
<dxg:GridControl.View>
<dxg:TableView>
<dxg:TableView.FormatConditions>
<dxg:FormatCondition Expression="[Deleted]" FieldName="Profit">
<dxc:Format Foreground="Red"/>
</dxg:FormatCondition>
</dxg:TableView.FormatConditions>
</dxg:TableView>
</dxg:GridControl.View>
</dxg:GridControl>
I created a style for a hyperlink control:
<Style x:Key="MyHyperlink" TargetType="{x:Type Hyperlink}">
<Setter Property="Foreground" Value="{StaticResource HyperlinkBrush}" />
<Setter Property="IsEnabled" Value="{Binding IsEnabled,RelativeSource={RelativeSource AncestorType={x:Type FrameworkElement}}}" />
<Style.Triggers>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Cursor" Value="Hand"/>
</Trigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True" >
<Setter Property="Foreground" Value="{StaticResource HyperlinkMouseOverBrush}" />
</Trigger>
</Style.Triggers>
</Style>
How can I use this style in a DataGridHyperlinkColumn?
The ElementStyle of this kind of column asks for a TextBlock style instead of an Hyperlink one...
<DataGridHyperlinkColumn EditingElementStyle="{StaticResource MyDataGridTextColumn}" ElementStyle="{StaticResource MyDataGridHyperlinkColumn}"
Header="WebSite" Binding="{Binding Site, NotifyOnValidationError=True,ValidatesOnDataErrors=True}" />
Remove the x:Key from your style and put it in DataGrid.Resources then it targets all Hyperlink controls within this DataGrid.
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).