如何从基类的ObserveableCollection显示子类的属性? [关闭]

问题描述 投票:0回答:1

我正在构建一个WPF应用程序,它只是团队成员编辑xml的UI。我的视图模型设置正常运行,直到我遇到这个问题。

假设我有一个模型“动物”,其中“狗”和“鸟”继承了它。

在我的viewmodel中,我有属性

public ObservableCollection<AnimalViewModel> Animals {get; set;}

在我看来,我在列表视图中显示每只动物,但对于鸟类,我希望有一个“CanFly”(企鹅和诸如此类)的复选框。我怎么能设置我的viewmodel和绑定来做到这一点?

以下是我当前设置的示例。

//MODELS
public class Animal
{
    public string Name;
}

public class Dog:Animal
{
    public string Breed;
}

public class Bird:Animal
{
    public bool CanFly;
}

//VIEWMODELS
//PropertyChanged.Fody nuget package
[AddINotifyPropertyChangedInterface]
public class BaseViewModel: INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = (sender, e) => { };
}

public class AnimalDataVM:BaseViewModel
{
    public ObservableCollection<AnimalVM> Animals{get;set;}
}

public class AnimalVM
{
    private Animal animal;
    public AnimalVM(Animal _animal)
    {
        animal = _animal;
    }

    public string Name
    {
        get
        {
            return animal.Name;
        }
        set
        {
            animal.Name = value;
        }
    }
}

public class BirdVM
{
    private Bird bird;
    public BirdVM(Bird _bird)
    {
        bird = _bird;
    }

    public string Name
    {
        get
        {
            return bird.Name;
        }
        set
        {
            bird.Name = value;
        }
    }

    public bool CanFly
    {
        get
        {
            return bird.CanFly;
        }
        set
        {
            bird.CanFly = value;
        }
    }
}
c# wpf inheritance mvvm observablecollection
1个回答
2
投票

第一种选择是使用DataTemplate。但这要求你在ViewModel中有不同的课程。然而,它非常有效且易于实施:

<ListView Name="MyListview">
    <ListView.Resources>
        <DataTemplate DataType="{x:Type local:AnimalViewModel}">
            <TextBlock Text="Animal"/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:DogViewModel}">
            <TextBlock Text="Dog"/>
        </DataTemplate>
        <DataTemplate DataType="{x:Type local:BirdViewModel}">
            <StackPanel Orientation="Horizontal">
                <CheckBox Content="Can fly"/>
                <TextBlock Text="Bird"/>
            </StackPanel>
        </DataTemplate>
    </ListView.Resources>
</ListView>

如果你的ViewModel不能有不同的类型,那么你应该使用Style DataTrigger和你的类属性:

<ListView Name="MyListview">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <CheckBox Content="Can fly">
                    <CheckBox.Style>
                        <Style TargetType="CheckBox">
                            <Setter Property="Visibility" Value="Collapsed"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding IsBird}" Value="True">
                                    <Setter Property="Visibility" Value="Visible"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </CheckBox.Style>
                </CheckBox>
                <TextBlock Text="Anything"/>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
© www.soinside.com 2019 - 2024. All rights reserved.