2

全部 -

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

我遇到的问题是:

1)我相信正在设置 DataTrigger 绑定中的源 - 但我仍然收到此错误: 在“对象”“字符串”上找不到 personId 属性。绑定表达式:路径=personId;DataItem='String' (HashCode=1896530089); 目标元素是 'Border' (Name=''); 目标属性是“NoTarget”(类型“对象”)

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
    }
}

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>

在此处输入图像描述

4

1 回答 1

5

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>
于 2012-09-17T15:10:30.000 回答