mvvm绑定selecteditem以更新listview [英] mvvm binding selecteditem to update listview

查看:66
本文介绍了mvvm绑定selecteditem以更新listview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVVM的新手,正在尝试将工作程序转换为MVVM程序.我一直在寻找答案,但是到目前为止还没有任何运气.

我基本上拥有的是:一个列表框和一个列表视图.列表框充满了火车站,我想在列表视图中查看车站的时间(有延迟等).列表框中充满了电台,每当我选择电台时,我都会被更新,并将其保存在名为"CurrentStation"的变量中.现在,我正在使用"CurrentStation"来获取该站点所有出发点的ObservableCollection列表,但是由于某种原因,该函数仅被调用一次,而在选择另一个站点时不会更新.

我也不知道在xaml代码中要绑定什么.

 < ListBox x:Name ="lstStations" Margin ="8" Grid.Row ="1" ItemsSource ="{Binding StationList}" SelectedItem ="{Binding CurrentStation}" DisplayMemberPath ="Name"/>< ListView Grid.Column ="1" Margin ="8" Grid.Row ="1" ItemsSource ="{绑定出发点}">< ListView.View>< GridView>< GridViewColumn DisplayMemberBinding ="{绑定时间,StringFormat = t}"标题=时间"/>< GridViewColumn DisplayMemberBinding ="{绑定延迟,转换器= {StaticResource mijnDelayConverter}}"标头="Delay"/>< GridViewColumn DisplayMemberBinding ="{Binding Station}" Header ="Station"/>< GridViewColumn DisplayMemberBinding ="{装订工具}" Header ="Vehicle"/>< GridViewColumn Header ="Platform" CellTemplate ="{DynamicResource dataTemplateTextblock}"/><!-没机会看这个^ ^虽然我不认为这是正确的-></GridView></ListView.View></ListView> 

这是ViewModel代码:

 公共字符串名称{得到{返回"MainPage";}}公共ObservableCollection< Station>电台列表{得到{返回Station.GetStations();}}私人车站_currentStation;公共车站CurrentStation{得到{返回_currentStation;}放{_currentStation =值;Console.WriteLine(已选择新站点:" + _currentStation.ToString());OnPropertyChanged("CurrentStation");}}私有ObservableCollection< Departure>_出港;公共ObservableCollection< Departure>出港{得到{返回Departure.GetDepartures(CurrentStation);}放{_departures =值;}} 

解决方案

我认为您需要:

  • 显式更新 Departures 属性,可以在 CurrentStation setter

    内部完成

      private station _currentStation;公共车站CurrentStation{得到{返回_currentStation;}放{_currentStation =值;出发=出发.GetDepartures(_currentStation);Console.WriteLine(已选择新站点:" + _currentStation.ToString());OnPropertyChanged("CurrentStation");}} 

  • 触发更改通知,该通知将使用著名的 OnPropertyChanged

    刷新您的出发绑定(和列表框!)

      private ObservableCollection< Departure>_出港;公共ObservableCollection< Departure>出港{得到{返回_departures}放{_departures =值;OnPropertyChanged("Departures");}} 

I'm new to MVVM and been trying to convert a working program to a MVVM program. I've been searching for an answer for this, but hadn't had any luck so far.

Basicly what I have is this: A listbox and a listview. The listbox is filled with Trainstations and I want the times from the station in a listview (with delay etc). The listbox is filled with stations and I whenever I select a station, it gets updated and I have it in a variable called 'CurrentStation'. Now I'm using this 'CurrentStation' to get an ObservableCollection list of all the departures from that station, but for some reason, the function is only called once and doesn't update when I select another station.

I also don't know what to bind in the xaml code.

<ListBox x:Name="lstStations" Margin="8" Grid.Row="1" ItemsSource="{Binding StationList}" SelectedItem="{Binding CurrentStation}" DisplayMemberPath="Name"/>
        <ListView Grid.Column="1" Margin="8" Grid.Row="1" ItemsSource="{Binding Departures}" >
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding Time, StringFormat=t}" Header="Time" />
                    <GridViewColumn DisplayMemberBinding="{Binding Delay, Converter={StaticResource mijnDelayConverter}}" Header="Delay"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Station}" Header="Station"/>
                    <GridViewColumn DisplayMemberBinding="{Binding Vehicle}" Header="Vehicle"/>
                    <GridViewColumn Header="Platform" CellTemplate="{DynamicResource dataTemplateTextblock}" />
<!-- Haven't had the chance to look at this ^ I don't think this is correct though -->
                    </GridView>
                </ListView.View>
            </ListView>

And here's the ViewModel code:

        public string Name
        {
            get
            {
                return "MainPage"; 
            }
        }
        public ObservableCollection<Station> StationList
        {
            get
            {
                return Station.GetStations();
            }
        }
        private Station _currentStation;
        public Station CurrentStation
        {
            get
            {
                return _currentStation;
            }
            set
            {
                _currentStation = value;
                Console.WriteLine("New station selected: " + _currentStation.ToString());
                OnPropertyChanged("CurrentStation");
            }
        }
        private ObservableCollection<Departure> _departures;
        public ObservableCollection<Departure> Departures
        {
            get
            {
                return Departure.GetDepartures(CurrentStation);
            }
            set
            {
                _departures = value;
            }
        }

解决方案

I think you need to :

  • Update the Departures property explicitly, it could be done inside the CurrentStation setter

    private Station _currentStation;
    public Station CurrentStation
    {
        get
        {
            return _currentStation;
        }
        set
        {
            _currentStation = value;
            Departures = Departure.GetDepartures(_currentStation);
            Console.WriteLine("New station selected: " + _currentStation.ToString());
            OnPropertyChanged("CurrentStation");
        }
    }
    

  • Trigger the change notification that will refresh your departures binding (and listbox!) with the famous OnPropertyChanged

    private ObservableCollection<Departure> _departures;
    public ObservableCollection<Departure> Departures
    {
        get
        {
            return _departures
        }
        set
        {
            _departures = value;
            OnPropertyChanged("Departures");
        }
    }
    

这篇关于mvvm绑定selecteditem以更新listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆