All –
I am trying to set public property in VM based on the current item in Observable collection (which is also in VM). So essentially – I want to set shadecolor as Blue or Pink based on the row I am (see sample code below). Also see image of what the end result will look like.
Can somebody please suggest – how I can achieve this – am really stuck with this problem
See sample code below:
Model.cs
public class Model
{
public Employee empdetails { get; set; }
}
public class Employee
{
public string fname { get; set; }
public string lname { get; set; }
public Enum gender { get; set; }
}
public enum gender
{
Male,
Female
}
ViewModel.cs
public class ViewModel
{
public ObservableCollection<Model> employees {get; set;}
public myCommand NextCommand { get; set; }
private Color _shadecolor;
public Color shadecolor
{
get
{
return _shadecolor;
}
set
{
_shadecolor = value;
}
}
public ViewModel()
{
employees = new ObservableCollection<Model>()
{
#region Populating Emp 1
new Model()
{
empdetails = new Employee()
{
fname = "John",
lname = "Smith",
gender = gender.Male
}
},
#endregion
#region Populating Emp 2
new Model()
{
empdetails = new Employee()
{
fname = "Robert",
lname = "Ally",
gender = gender.Female
}
},
#endregion
};
NextCommand = new myCommand(myNextCommandExecute, myCanNextCommandExecute);
}
private void myNextCommandExecute(object parameter)
{
}
private bool myCanNextCommandExecute(object parameter)
{
return true;
}
}
View.xaml
<Window x:Class="WpfApplication1.View"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="View" Height="500" Width="500" WindowStyle="None" AllowsTransparency="True" Background="Transparent">
<Border VerticalAlignment="Top" HorizontalAlignment="Left" BorderBrush="Silver" BorderThickness="2" CornerRadius="15">
<Border.Background>
<LinearGradientBrush StartPoint="0.5,0" EndPoint="0.511,0.957">
<GradientStop Color="LightGray" Offset="0.55" />
<GradientStop Color="{Binding shadecolor}" Offset="1.3" />
</LinearGradientBrush>
</Border.Background>
<Grid Width="300" Height="300" Margin="3">
<StackPanel VerticalAlignment="Top" >
<TextBlock Text="{Binding Path=employees/empdetails.fname}" />
<Button Command="{Binding NextCommand}" Content="Next" Width="100"></Button>
</StackPanel>
</Grid>
</Border>
</Window>

I believe what you want is to bind SelectedItem={Binding SelectedItem} where Selected item is on the view model as well, exposed as an observable property.
I’m not totally sure what you are trying to achieve here though as you don’t have anything in your XAML deriving from Selector, therefore there is no concept of a selected item here.