简体   繁体   English

WPF - MVVM - XAML 绑定问题

[英]WPF - MVVM - XAML Binding problems

I'm doing a simple WPF application which has a first ObservableList of ViewModels:我正在做一个简单的 WPF 应用程序,它有一个 ViewModels 的第一个 ObservableList:

public OuterViewModel CurrentItem
{
    get;
    set;
}

public ObservableCollection<OuterViewModel> OuterItems
{
    get;
    set;
}

The OuterViewModel has these objects: OuterViewModel 有这些对象:

 public string Description
 {
      get;
      private set;
 }

 public ObservableCollection<InnerViewModel> Ruote
 {
    get;
    private set;
 }

The inner view model:内部视图模型:

public string Name
{
get;
set;
}

public Numbers Row
{
    get;
    set;
}

The Numbers object has simply five numeric properties. Numbers 对象只有五个数字属性。 About the XAML:关于 XAML:

<Window.DataContext>
    <local:MainWindowViewModel />
</Window.DataContext>
<ListBox Grid.Column="0" ItemsSource="{Binding OuterItems}" SelectedItem="{Binding CurrentItem}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <Label Content="{Binding Name}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
<ListView Grid.Column="1" ItemsSource="{Binding CurrentItem.Ruote}">
        <ListView.View>
            <GridView>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding ?}"/>
                <GridViewColumn Header="Primo" />
                <GridViewColumn Header="Secondo" />
                <GridViewColumn Header="Terzo" />
                <GridViewColumn Header="Quarto" />
                <GridViewColumn Header="Quinto" />
            </GridView>
        </ListView.View>
    </ListView>

The Listbox shows correctly, while the Intellisense says that I can select CurrentItem.Ruote for ListView.ItemSource.列表框显示正确,而智能感知说我可以为 ListView.ItemSource 选择 CurrentItem.Ruote。 Now I want to fill the rows of ListView with Ruote items and here comes the troubles: I can't select the InnerViewModel properties to put in the cells.现在我想用 Ruote 项目填充 ListView 的行,但麻烦来了:我无法选择 InnerViewModel 属性来放入单元格中。 What am I doing wrong?我究竟做错了什么?

You need something to communicate that properties changed.您需要一些东西来传达属性已更改。

You can implement the INotifyPropertyChanged interface.您可以实现 INotifyPropertyChanged 接口。 For example:例如:

public class ViewModel : INotifyPropertyChanged
{
    private OuterViewModel currentItem;
    public OuterViewModel CurrentItem
    {
        get { return currentItem; }
        set
        {
            currentItem = value;
            NotifyPropertyChanged(nameof(CurrentItem));
        }
    }

    private void NotifyPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM