XAML表单使用第二个选取器根据第一个选取器中得选择进行排序

5lwkijsr  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(118)

I have 2 dropdowns (pickers) on a XAML form. The first is an ObservabelCollection of Territories. The second is an ObservableCollection of type Tracks. When the form loads my ViewModel loads both collections and each collection is bound to a picker. I want to filter and display only those tracks that are associated with the selected territory in the second picker. FYI-the second picker is disabled until a selection is made in the first. Actually I don't require that the second picker's data source be set as the form loads, unless the solution requires it. The selection in the first picker will be the key to filter the data for the second. I have tried to handle the filtering in the Territory picker's SelectedIndexChanged event but my ObservableCollection 'tracks' is not exposed here.

private void territoryPicker_SelectedIndexChanged(object sender, EventArgs e)
    {
        var picker = (Picker)sender;
        string territory = picker.SelectedItem.ToString();

        Tracks _track = tracks.Where(X => X.Territory = territory);<<<==== Does not work
        trackPicker.ItemsSource = _track.Track;<<<==== Does not work

        trackPicker.IsEnabled = true;

    }

I've also tried to not build the Tracks OC until after the Territory is selected like this:

private void territoryPicker_SelectedIndexChanged(object sender, EventArgs e)
    {
        var picker = (Picker)sender;
        string territory = picker.SelectedItem.ToString();

        TrackViewModel vm = new TrackViewModel(); 
        var _tracks = (Tracks)vm.Tracks;  <<<<<==== This does not work

        trackPicker.IsEnabled = true;

    }

The ViewModel runs and the tracks are loaded via the API but when it returns here Tracks is empty. I'm open to a reasonable solution (not 3rd party controls/packages) that will accomplish this task . Thanks

wz1wpwve

wz1wpwve1#

我在研究另一个问题时偶然发现了答案。我一直在{content]. xaml页面中引用我的viewmodel,就像这样。

<ContentPage.BindingContext>
    <vm:TrackViewModel />
</ContentPage.BindingContext>

当这样做的时候,VM的资源在代码后面是不可用的。但是当我在页面构造函数中引用VM的时候,就像这样:

public partial class RequestmyTracks : ContentPage
{
    TrackViewModel trackviewmodel = new TrackViewModel();

然后在这里绑定:

public RequestmyTracks()
    {
        InitializeComponent();
        BindingContext = trackviewmodel;
    }

trackviewmodel可以在SelectedIndexChanged事件中访问,其他一切都非常简单,如下所示:

private void territoryPicker_SelectedIndexChanged(object sender, EventArgs e)
    {
        var picker = (Picker)sender;
        string territory = picker.SelectedItem.ToString();
        ObservableCollection<Tracks> dah = (ObservableCollection<Tracks>)trackviewmodel.Tracks;
        List<string> trackNames = new List<string>();

        foreach (var track in dah)
        {
            if (track.Territory == territory)
                trackNames.Add(track.Track);
        }
        trackPicker.ItemsSource = trackNames;
        if(trackNames.Count ==1)
            trackPicker.SelectedIndex = 0;

        trackPicker.IsEnabled = true;
    }

谢谢你的帮助

相关问题