Click here to Skip to main content
14,984,194 members
Home / Discussions / WPF
   

WPF

 
GeneralRe: ComboBox Control Template Problem Pin
Kevin Marois20-Apr-17 4:38
professionalKevin Marois20-Apr-17 4:38 
QuestionWPF Enum DP - The default value type does not match the type of the property Pin
Kevin Marois18-Apr-17 7:21
professionalKevin Marois18-Apr-17 7:21 
AnswerRe: WPF Enum DP - The default value type does not match the type of the property Pin
Pete O'Hanlon19-Apr-17 23:34
mvePete O'Hanlon19-Apr-17 23:34 
QuestionTimeline Thumb Style Pin
Kevin Marois18-Apr-17 6:19
professionalKevin Marois18-Apr-17 6:19 
QuestionInterpret an Error Pin
Mycroft Holmes28-Mar-17 22:31
professionalMycroft Holmes28-Mar-17 22:31 
AnswerRe: Interpret an Error - Resolved Pin
Mycroft Holmes28-Mar-17 22:51
professionalMycroft Holmes28-Mar-17 22:51 
QuestionBinding Delay Pin
Mycroft Holmes20-Mar-17 17:14
professionalMycroft Holmes20-Mar-17 17:14 
QuestionFormating DataGrid cell depending on content of multiple cells Pin
kilauea-de5-Mar-17 9:17
Memberkilauea-de5-Mar-17 9:17 
Hi there,

I have a DataGrid with it's content stored in a List<>. I'm able to format cells depending on the data of ONE other cell using a value converter.

Here is the my MainWindow.xaml:
<Window x:Class="Test_WpfGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Test_WpfGrid"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <local:CBrushConverter x:Key="BrushConverter"/>
    </Window.Resources>
    <Grid>
        <DataGrid x:Name="grdPeople" Margin="5,5,5,5" AutoGenerateColumns="False" SelectionMode="Single" SelectionUnit="FullRow">
            <DataGrid.Columns>
                <DataGridTextColumn Width="150"  Binding="{Binding FName}" Header="First Name">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="Background" Value="{Binding Age, Converter={StaticResource BrushConverter}}" />
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Width="150"  Binding="{Binding LName}" Header="Last Name">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Setter Property="Background" Value="{Binding Age, Converter={StaticResource BrushConverter}}" />
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
                <DataGridTextColumn Width="50"  Binding="{Binding Age}" Header="Age" />
                <DataGridTextColumn Width="50"  Binding="{Binding DisplState}" Header="State" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>


And this is the MainWindow.xaml.cs:
namespace Test_WpfGrid
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<CPerson> People = new List<CPerson>();
            People.Add(new CPerson("John",  "Doe",   25, 2));
            People.Add(new CPerson("Jane",  "Doe",   52, 4));
            People.Add(new CPerson("Jenny", "Doe",   14, 0));
            People.Add(new CPerson("Sammy", "Doe",   24, 3));
            People.Add(new CPerson("Paul",  "Smith", 10, 3));
            grdPeople.ItemsSource = People;
        }
    }
}


Data is stored in a List<cperson> with the following class:
namespace Test_WpfGrid
{
    public class CPerson
    {
        public string FName { get; set; }
        public string LName { get; set; }
        public int Age { get; set; }
        public int State { get; set; }

        public string DisplState
        {
            get { return (this.State > 0) ? this.State.ToString() : "undef"; }
        }

        public CPerson(string FName, string LName, int Age, int State)
        {
            this.FName = FName;
            this.LName = LName;
            this.Age = Age;
            this.State = State;
        }
    }
}


And finally here is my value converter class:

namespace Test_WpfGrid
{
    public class CBrushConverter : IValueConverter
    {
        private Color[] mBGColors = { Color.FromArgb(255, 224, 224, 224),   // grey
                                      Color.FromArgb(255, 255, 155, 155),   // red
                                      Color.FromArgb(255, 200, 240, 200)    // green
                                    };

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            int Age = (int)value;
            System.Windows.Media.Brush Brush = new SolidColorBrush(this.mBGColors[(Age >= 18) ? 2 : ((Age >= 12) ? 1 : 0)]);
            return Brush;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}


With this code the columns for FName and LName are formated with a specific background color, depending on the value of the Age-property.

Now I want to format the two columns depeding on the values of Age AND State.
Is this possible?
What has to be changed in my code?

Thanks for your help!
AnswerRe: Formating DataGrid cell depending on content of multiple cells Pin
Richard Deeming5-Mar-17 23:57
mveRichard Deeming5-Mar-17 23:57 
GeneralRe: Formating DataGrid cell depending on content of multiple cells Pin
kilauea-de15-Mar-17 2:54
Memberkilauea-de15-Mar-17 2:54 
QuestionHow can I set my SQL stored procedure variables through WPF textboxs? Pin
Magerager3-Mar-17 4:03
MemberMagerager3-Mar-17 4:03 
AnswerRe: How can I set my SQL stored procedure variables through WPF textboxs? Pin
Richard Deeming3-Mar-17 4:25
mveRichard Deeming3-Mar-17 4:25 
GeneralRe: How can I set my SQL stored procedure variables through WPF textboxs? Pin
Magerager3-Mar-17 12:29
MemberMagerager3-Mar-17 12:29 
QuestionJavaScript Error - Internet Explorer - Object doesn't support property or method 'indexOf' Pin
Kevin Marois2-Mar-17 10:35
professionalKevin Marois2-Mar-17 10:35 
SuggestionRe: JavaScript Error - Internet Explorer - Object doesn't support property or method 'indexOf' Pin
Richard Deeming2-Mar-17 10:49
mveRichard Deeming2-Mar-17 10:49 
GeneralRe: JavaScript Error - Internet Explorer - Object doesn't support property or method 'indexOf' Pin
Kevin Marois2-Mar-17 10:51
professionalKevin Marois2-Mar-17 10:51 
GeneralRe: JavaScript Error - Internet Explorer - Object doesn't support property or method 'indexOf' Pin
Kevin Marois2-Mar-17 10:52
professionalKevin Marois2-Mar-17 10:52 
GeneralRe: JavaScript Error - Internet Explorer - Object doesn't support property or method 'indexOf' Pin
Richard Deeming3-Mar-17 2:32
mveRichard Deeming3-Mar-17 2:32 
QuestionJQuery Not Found Pin
Kevin Marois2-Mar-17 8:25
professionalKevin Marois2-Mar-17 8:25 
AnswerRe: JQuery Not Found Pin
Sander Rossel2-Mar-17 8:37
professionalSander Rossel2-Mar-17 8:37 
GeneralRe: JQuery Not Found Pin
Kevin Marois2-Mar-17 10:02
professionalKevin Marois2-Mar-17 10:02 
GeneralRe: JQuery Not Found Pin
Sander Rossel2-Mar-17 10:16
professionalSander Rossel2-Mar-17 10:16 
GeneralRe: JQuery Not Found Pin
Kevin Marois2-Mar-17 10:29
professionalKevin Marois2-Mar-17 10:29 
GeneralRe: JQuery Not Found Pin
Sander Rossel2-Mar-17 10:32
professionalSander Rossel2-Mar-17 10:32 
AnswerRe: JQuery Not Found Pin
ZurdoDev2-Mar-17 10:16
professionalZurdoDev2-Mar-17 10:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.