WPF Style Trigger on Enum Property Value

J

jehugaleahsa

<Window x:Class="TestWPF.Main"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/
presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:TestWPF"
Title="Main"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
SizeToContent="WidthAndHeight">

<Window.Resources>
<c:people x:Key="PeopleData" />
<Style TargetType="{x:Type Button}">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True" >
<Setter Property="Background" Value="Yellow" />
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Content" Value="Male"/>
</MultiTrigger.Conditions>
<Setter Property="Background" Value="Blue" />
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="Content" Value="Female" /</MultiTrigger.Conditions>
<Setter Property="Background" Value="Pink" />
</MultiTrigger>
</Style.Triggers>
</Style>

<DataTemplate DataType="{x:Type c:person}">
<StackPanel Orientation="Horizontal">
<Button Name="_nameLabel" Content="{Binding
Path=Name}" Width="250"/>
<Button Name="_ageLabel" Content="{Binding Path=Age}"
Width="250" />
<Button Name="_sexLabel" Content="{Binding Path=Sex}"
Width="250" />
</StackPanel>
</DataTemplate>

</Window.Resources>

<ListBox ItemsSource="{Binding Source={StaticResource
PeopleData}}" />
</Window>


I have a stupid example I would like to get working. I have buttons
that, when pressed, I would like to change color. If the bound
Person's sex (an enum value) is female I would like the button to turn
one color, when it's a male, another. I'm not entirely sure how to
check that Content property when the value is something other than a
string.

public enum Sex
{
Male,
Female
}

public class Person
{
public Person(string name, int age, Sex sex)
{
Name = name;
Age = age;
Sex = sex;
}

public string Name
{
get;
set;
}

public int Age
{
get;
set;
}

public Sex Sex
{
get;
set;
}
}

public class People : ObservableCollection<Person>
{
}

public partial class Main : Window
{
public Main()
{
InitializeComponent();
People people = (People)Resources["PeopleData"]; // why
can't I just create a private readonly People?
people.Add(new Person("Bob", 21, Sex.Male));
people.Add(new Person("Tammy", 34, Sex.Female));
people.Add(new Person("Chuck", 53, Sex.Male));
}
}

Thanks,
Travis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top