全部 -
我正在尝试根据 Observable 集合中的当前项目(也在 VM 中)在 VM 中设置公共属性。所以本质上 - 我想根据我所在的行将 shadecolor 设置为蓝色或粉红色(参见下面的示例代码)。另请参阅最终结果的图像。
有人可以建议 - 我怎么能做到这一点 - 我真的被这个问题困住了
请参阅下面的示例代码:
模型.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
}
视图模型.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;
}
}
查看.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>