数据触发器绑定问题(使用Int和Enum)

时间:2012-09-17 15:05:33

标签: wpf xaml data-binding

全部 -

需要对此问题有所了解。我创建了一个示例项目来隔离我的一个项目的问题。我试图根据模型中的某个值(Int / String / Enum)设置Border的背景。

我遇到的问题是:

1)我相信DataTrigger Binding中的源代码正在设置中 - 但我仍然收到此错误: 在'object'''String'上找不到 personId属性。 BindingExpression:路径= PERSONID; DataItem ='String'(HashCode = 1896530089); target元素是'Border'(Name =''); target属性为'NoTarget'(类型'Object')

2)我在int(或字符串)上执行此操作只是为了使概念起作用。但实际上我需要使用Enum值。对此的任何建议也会有所帮助。

以下是代码段:

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>

enter image description here

1 个答案:

答案 0 :(得分:5)

人是一个可观察的集合,这是你在数据触发器中绑定的东西。 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>