I have a datagrid with my total production divided into days and I have a goal to meet per week.
I need that if on Monday the goal was not met, the cell will be painted yellow and the same will happen until it is met and painted green. My goal is enter it manually in a textbox.
I get the value of the datatrigger from my query to my database, if it is 1 it is because it is in the current week, 0 if it is not.
It is currently green because it indicates what week we are currently in, how could I do what I want? Annex my code with which paints the week in which we are and the production made per day, and it should be clarified that
I am new working with wpf.
I hope you can help me, thanks.
<DataGridTextColumn Binding="{Binding semana}" Header="Semana">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding true}" Value="1">
<Setter Property="Background" Value="Green"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding true}" Value="0">
<Setter Property="Background" Value="LightGray"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Lunes}" Header="Lunes">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding true}" Value="1">
<Setter Property="Background" Value="green"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding true}" Value="0">
<Setter Property="Background" Value="LightGray"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Lunes}" Value="">
<Setter Property="Background" Value="Green"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Lunes}" Value="">
<Setter Property="Background" Value="LightGray"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Martes}" Header="Martes">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding true}" Value="1">
<Setter Property="Background" Value="green"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding true}" Value="0">
<Setter Property="Background" Value="LightGray"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Martes}" Value="0">
<Setter Property="Background" Value="LightGray"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Miercoles}" Header="Miércoles">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding true}" Value="1">
<Setter Property="Background" Value="green"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding true}" Value="0">
<Setter Property="Background" Value="LightGray"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Miercoles}" Value="0">
<Setter Property="Background" Value="LightGray"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Jueves}" Header="Jueves">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding true}" Value="1">
<Setter Property="Background" Value="green"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Jueves}" Value="0">
<Setter Property="Background" Value="LightGray"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding true}" Value="0">
<Setter Property="Background" Value="LightGray"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Viernes}" Header="Viernes">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding true}" Value="1">
<Setter Property="Background" Value="green"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding true}" Value="0">
<Setter Property="Background" Value="LightGray"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Viernes}" Value="0">
<Setter Property="Background" Value="LightGray"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Sabado}" Header="Sábado">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding true}" Value="1">
<Setter Property="Background" Value="green"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding true}" Value="0">
<Setter Property="Background" Value="LightGray"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Sabado}" Value="0">
<Setter Property="Background" Value="LightGray"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Total}" Header="total">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding true}" Value="1">
<Setter Property="Background" Value="green"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding true}" Value="0">
<Setter Property="Background" Value="LightGray"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding total}" Value="0">
<Setter Property="Background" Value="LightGray"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
Related
I need to disable editing on specific column based on a trigger ,but is not working.
<DataGridTextColumn Binding="{Binding Qty,UpdateSourceTrigger=PropertyChanged}" Header="QTY" Width="100">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="HorizontalAlignment" Value="Right"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding ItemType}" Value="Discount">
<Setter Property="IsEnabled" Value="False"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
Add this:
<DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="HorizontalAlignment" Value="Right"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding ItemType}" Value="Discount">
<Setter Property="IsEnabled" Value="False"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.EditingElementStyle>
I have ListView with my object and one of my object properties is of bool value:
<GridViewColumn Width="100" Header="FileCheck " DisplayMemberBinding="{Binding IsFileOK}" />
Instead of displaying this variable value (true or false) how can I replace this with my own text and my own color ?
For example:
File is OK // green color
File damage // red color
This is what i have try:
<Style TargetType="ListViewItem">
<Style.Triggers>
<DataTrigger Binding="{Binding IsFileOK}" Value="false">
<Setter Property="Background" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding IsFileOK}" Value="true">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
Try this one
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsFileOK}" Value="False">
<Setter Property="Background" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding IsFileOK}" Value="True">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
Idea is to modify the background and text using Style and Triggers.
e.g.
<ListView.Resources>
<Style TargetType="ListViewItem">
<Setter Property="Background" Value="Blue"></Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="Red"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</ListView.Resources>
So you can replace Trigger with DataTrigger to use properties from view model.
Use below code to achieve this,
<ListView.Resources>
<Style TargetType="ListViewItem">
<Setter Property="Background" Value="Blue"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding IsFileOK}" Value="True">
<Setter Property="Background" Value="Red"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.Resources>
Is it possible to reduce this to a single trigger?
<Style x:Key="StatusTextBlock" TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Text,
RelativeSource={RelativeSource Self}}" Value="Running">
<Setter Property="Foreground" Value="Green"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Text,
RelativeSource={RelativeSource Self}}" Value="Paused">
<Setter Property="Foreground" Value="Yellow"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Text,
RelativeSource={RelativeSource Self}}" Value="Stopped">
<Setter Property="Foreground" Value="Red"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
If "Running" and "Stopped" are the only two available cases, you can set one as a property, and only use one trigger:
<Style x:Key="StatusTextBlock" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="Green" /> <!--default case == Running -->
<Style.Triggers>
<DataTrigger Binding="{Binding Path=Text,
RelativeSource={RelativeSource Self}}" Value="Stopped">
<Setter Property="Foreground" Value="Red"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
Is it possible to set an inline style when I've already set a ResourceDictionary?
Here is what I've already set...
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/My.Project.Common.Desktop;component/Themes/StandardStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
Here are the styles I need to add to the UserControl.Resources node...
<Style x:Key="MessageErrorIcon" TargetType="{x:Type Rectangle}">
<Style.Triggers>
<DataTrigger Binding="{Binding Icon}" Value="Asterisk">
<Setter Property="Fill" Value="{DynamicResource MessageOverlayInformationIcon}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Icon}" Value="Error">
<Setter Property="Fill" Value="{DynamicResource MessageOverlayErrorIcon}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Icon}" Value="Exclamation">
<Setter Property="Fill" Value="{DynamicResource MessageOverlayExclamationIcon}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Icon}" Value="Hand">
<Setter Property="Fill" Value="{DynamicResource MessageOverlayErrorIcon}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Icon}" Value="Information">
<Setter Property="Fill" Value="{DynamicResource MessageOverlayInformationIcon}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Icon}" Value="Question">
<Setter Property="Fill" Value="{DynamicResource MessageOverlayQuestionIcon}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Icon}" Value="Stop">
<Setter Property="Fill" Value="{DynamicResource MessageOverlayErrorIcon}"/>
</DataTrigger>
<DataTrigger Binding="{Binding Icon}" Value="Warning">
<Setter Property="Fill" Value="{DynamicResource MessageOverlayExclamationIcon}"/>
</DataTrigger>
</Style.Triggers>
</Style>
I need it to be set these styles at a UserControl level because they are bound to the data context.
How do I do this?
you can add resources like this
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/My.Project.Common.Desktop;component/Themes/StandardStyles.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="MessageErrorIcon"
TargetType="{x:Type Rectangle}">
<Style.Triggers>
<DataTrigger Binding="{Binding Icon}"
Value="Asterisk">
<Setter Property="Fill"
Value="{DynamicResource MessageOverlayInformationIcon}" />
</DataTrigger>
<DataTrigger Binding="{Binding Icon}"
Value="Error">
<Setter Property="Fill"
Value="{DynamicResource MessageOverlayErrorIcon}" />
</DataTrigger>
<DataTrigger Binding="{Binding Icon}"
Value="Exclamation">
<Setter Property="Fill"
Value="{DynamicResource MessageOverlayExclamationIcon}" />
</DataTrigger>
<DataTrigger Binding="{Binding Icon}"
Value="Hand">
<Setter Property="Fill"
Value="{DynamicResource MessageOverlayErrorIcon}" />
</DataTrigger>
<DataTrigger Binding="{Binding Icon}"
Value="Information">
<Setter Property="Fill"
Value="{DynamicResource MessageOverlayInformationIcon}" />
</DataTrigger>
<DataTrigger Binding="{Binding Icon}"
Value="Question">
<Setter Property="Fill"
Value="{DynamicResource MessageOverlayQuestionIcon}" />
</DataTrigger>
<DataTrigger Binding="{Binding Icon}"
Value="Stop">
<Setter Property="Fill"
Value="{DynamicResource MessageOverlayErrorIcon}" />
</DataTrigger>
<DataTrigger Binding="{Binding Icon}"
Value="Warning">
<Setter Property="Fill"
Value="{DynamicResource MessageOverlayExclamationIcon}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ResourceDictionary>
</UserControl.Resources>
How can I make columns coloring in DataGrid if I also want use AlternatingRowBackground property?
I have some ideas, but it doesn't work :(.
<de:DataGrid Name="dataGrid1"
AlternationCount="2"
AlternatingRowBackground="Salmon"
>
<de:DataGrid.Columns>
<de:DataGridTextColumn Binding="{Binding Path=Phrase}"
Header="Phrase">
<de:DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<Trigger Property="ItemsControl.AlternationIndex" Value="0">
<Setter Property="Background" Value="Green"></Setter>
</Trigger>
<Trigger Property="ItemsControl.AlternationIndex" Value="1">
<Setter Property="Background" Value="Red"></Setter>
</Trigger>
</Style.Triggers>
</Style>
</de:DataGridTextColumn.ElementStyle>
</de:DataGridTextColumn>
</de:DataGrid.Columns>
</de:DataGrid>
Maybe somebody knows working solution? Thanks.
You are searching AlternationIndex property in the wrong control. This property belongs to DataGridRow.
<DataGrid ItemsSource="{Binding}" AlternationCount="2" AutoGenerateColumns="False" AlternatingRowBackground="Salmon">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Path=Phrase}" Header="Phrase">
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Style.Triggers>
<DataTrigger Binding="{Binding AlternationIndex, RelativeSource={RelativeSource AncestorType=DataGridRow}}" Value="0">
<Setter Property="Background" Value="Green"></Setter>
</DataTrigger>
<DataTrigger Binding="{Binding AlternationIndex, RelativeSource={RelativeSource AncestorType=DataGridRow}}" Value="1">
<Setter Property="Background" Value="Red"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>