如何绑定到ViewModel中的集合

本文关键字:ViewModel 集合 何绑定 绑定 | 更新日期: 2023-09-27 18:29:43

ViewModel中的一个属性是对象的集合。我不知道如何将我的内部DataTemplate绑定到集合中这些对象的属性:有问题的绑定在TestDataTemplate1中,它就是找不到N和STR

<Page.Resources>
    <DataTemplate x:Key="TestDataTemplate1">
        <Grid Background="Cornsilk" HorizontalAlignment="Stretch" Height="Auto">
            <TextBlock x:Name="LeftTB" Text="{Binding N}" Foreground="Red" HorizontalAlignment="Left" VerticalAlignment="Stretch"/>
            <TextBlock x:Name="RightTB" Text="{Binding STR}" Foreground="Green" HorizontalAlignment="Right" VerticalAlignment="Stretch"/>
        </Grid>
    </DataTemplate>
    <DataTemplate x:Key="TestDataTemplate">
        <Grid Background="Yellow" HorizontalAlignment="Stretch" Height="Auto">
            <TextBlock x:Name="LeftTB" Text="{Binding Name}" HorizontalAlignment="Left" VerticalAlignment="Center"/>
            <ListBox x:Name="MyListBox" x:FieldModifier="Public"
             ItemsSource="{Binding SampleCollection}"
             ItemTemplate="{StaticResource TestDataTemplate1}"
             Background="Blue"
             HorizontalAlignment="Center"
             VerticalAlignment="Stretch" Width="200">
                <ListBox.ItemContainerStyle>
                    <Style TargetType="ListBoxItem">
                        <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
                    </Style>
                </ListBox.ItemContainerStyle>
            </ListBox>
            <TextBlock x:Name="RightTB" Text="{Binding Age}" HorizontalAlignment="Right" VerticalAlignment="Center"/>
        </Grid>
    </DataTemplate>
</Page.Resources>

这是我的类型,包含以下属性:

class AlaBalaVM : INotifyPropertyChanged
{
    AlaBala _ab = null;
    public AlaBalaVM(AlaBala ab)
    {
        _ab = ab;
    }
    public string N
    {
        get
        {
            return "num: " + _ab._n.ToString();
        }
        set { RaisePropertyChanged("N"); }
    }
    public string STR
    {
        get
        {
            return _ab._str.ToString() + " string";
        }
        set { RaisePropertyChanged("STR"); }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

这是我的ViewModel,我在其中填充DataSource集合:

public class PersonVMWrapper : INotifyPropertyChanged
{
    PersonModel _pm = null;
    public PersonVMWrapper(PersonModel pm)
    {
        _pm = pm;
    }
    public string Name
    {
        get
        {
            return "mr." + _pm.Name;
        }
        set { RaisePropertyChanged("Name"); }
    }
    public string Age
    {
        get
        {
            return _pm.Age.ToString() + " years";
        }
        set { RaisePropertyChanged("Age"); }
    }
    public ObservableCollection<AlaBala> SampleCollection
    {
        get
        {
            return _pm.SampleCollection;
        }
        set { RaisePropertyChanged("SampleCollection"); }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
public class PersonVM : INotifyPropertyChanged
{
    DispatcherTimer _refreshTimer = new DispatcherTimer();
    private ObservableCollection<PersonVMWrapper> personDataSource;
    public PersonModel PM { get; set; }
    public PersonVM()
    {
        AddItemToDataSource(null);
    }
    private void AddItemToDataSource(object o)
    {
        DataSource.Add(new PersonVMWrapper(new PersonModel() { Name = "asdasd", Age = 23, SampleCollection = new ObservableCollection<AlaBala> { new AlaBala(3, "e1"), new AlaBala(4, "e2") } }));
    }
    public ObservableCollection<PersonVMWrapper> DataSource
    {
        get
        {
            if (this.personDataSource == null)
            {
                this.personDataSource = new ObservableCollection<PersonVMWrapper>();
            }
            return this.personDataSource;
        }
        set
        {
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

如何绑定到ViewModel中的集合

您还没有发布"Alabala"类的定义——它包含"N"answers"STR"属性吗?正如@KooKiz所指出的,看起来你是想让"SampleCollection"成为ObservableCollection<AlaBalaVM>,即

SampleCollection = new ObservableCollection<AlaBalaVM> 
{
    new AlaBalaVM(new AlaBala(3, "e1")), 
    new AlaBalaVM(new AlaBala(4, "e2")),
}