All –
Need some insight into this issue. I have created a sample project to isolate the issue on one of my projects. I am trying to set the background of my Border based on a certain value in the model (Int/String/Enum).
The issue I am having is:
1) I believe the source in the DataTrigger Binding is being set – but I am still getting this error:
personId property not found on ‘object’ ”String’. BindingExpression:Path=personId; DataItem=’String’ (HashCode=1896530089); target element is ‘Border’ (Name=”); target property is ‘NoTarget’ (type ‘Object’)
2) I was doing this on int (or string) just to make the concept work. But realistically I need to make this of the Enum value. any suggestions on this will also help.
Here are the code snippets:
Person.cs
public class Person
{
public int personId { get; set; }
public string personName { get; set; }
public Gender personType { get; set; }
public enum Gender
{
Male,
Female
}
}
PersonViewModel.cs
public class PersonViewModel
{
private ObservableCollection<Person> _people;
public ObservableCollection<Person> people
{
get
{
return _people;
}
}
public PersonViewModel()
{
_people = new ObservableCollection<Person>()
{
new Person()
{
personId = 1,
personName = "John",
personType = Person.Gender.Male
},
new Person()
{
personId = 1,
personName = "Mary",
personType = Person.Gender.Female
},
};
}
}
PersonView.xaml.cs
public PersonView()
{
InitializeComponent();
var personvm = new PersonViewModel();
DataContext = personvm;
}
PersonView.xaml
<Window.Resources>
<Style x:Key="BorderGradient" TargetType="{x:Type Border}">
<Style.Triggers>
<DataTrigger Binding="{Binding Source=people, Path=personId}" Value="1">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.511,0.957">
<GradientStop Color="LightGray" Offset="0.55" />
<GradientStop Color="Black" Offset="1.3" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</DataTrigger>
<DataTrigger Binding="{Binding Source=people, Path=personId}" Value="2">
<Setter Property="Background">
<Setter.Value>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.511,0.957">
<GradientStop Color="LightGray" Offset="0.55" />
<GradientStop Color="Yellow" Offset="1.3" />
</LinearGradientBrush>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Border CornerRadius="15,15,15,15" Style="{StaticResource BorderGradient}" >
<Grid Width="Auto" MinWidth="750" Height="Auto" MinHeight="600">
<!-- Logic in VM where visibility would be set through VM -->
<StackPanel Grid.Row="0" HorizontalAlignment="Center" VerticalAlignment="Top" Visibility="To be set" >
<loc:MaleView/>
</StackPanel>
<StackPanel Grid.Row="1" HorizontalAlignment="Center" Visibility=" to be set">
<loc:FemaleView />
</StackPanel>
</Grid>
</Border>

people is an observable collection, which is what you’re binding to in your datatrigger. The ObservableCollection doesn’t have a property named “personId”.
It would probably be better to have a DataTemplate for your PersonViewModel