Pat*_*ick 2 data-binding wpf xaml
全部-
需要对此问题有一些了解。我创建了一个示例项目,以将问题隔离到我的一个项目中。我试图基于模型中的某个值(Int / String / Enum)设置Border的背景。
我遇到的问题是:
1)我相信正在设置DataTrigger绑定中的源-但我仍然收到此错误:在' object''String '上找不到personId属性。BindingExpression:Path = personId; DataItem ='字符串'(HashCode = 1896530089); 目标元素是'Border'(Name =''); 目标属性为“ NoTarget”(类型“ Object”)
2)我在int(或字符串)上执行此操作只是为了使概念起作用。但实际上,我需要将此作为Enum值。关于此的任何建议也将有所帮助。
以下是代码片段:
人.cs
public class Person
{
public int personId { get; set; }
public string personName { get; set; }
public Gender personType { get; set; }
public enum Gender
{
Male,
Female
}
}
Run Code Online (Sandbox Code Playgroud)
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
},
};
}
}
Run Code Online (Sandbox Code Playgroud)
PersonView.xaml.cs
public PersonView()
{
InitializeComponent();
var personvm = new PersonViewModel();
DataContext = personvm;
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)

people是一个可观察的集合,这是您在数据触发器中绑定的对象。ObservableCollection没有名为“ personId”的属性。
为您的PersonViewModel设置一个DataTemplate可能会更好
<DataTemplate DataType={x:Type vm:PersonViewModel}">
<Grid Background="Red">
.. Template For PersonViewModel Here ..
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=personId}" Value="1">
.... do stuff
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3211 次 |
| 最近记录: |