条件组合框无法正常工作

时间:2019-05-10 01:54:40

标签: c# uwp

我正在开发用C#编写的uwp应用程序。我的应用使用prism.windows

该应用程序有两个组合框。第一个是选择国家。下一个显示在第一个组合框中选择的国家的城市。

我在第一个组合框中选择“日本”,在下一个组合框中显示“东京”。接下来,我选择美国,下一个仍然显示东京。

下一个应该显示芝加哥和代顿。

namespace TestComboBox2.ViewModels
{
    public class MainPageViewModel:BindableBase
    {
        public Views.MainPage View { get; private set; } = null;

        public void Initialize(Views.MainPage mainPage)
        {
            View = mainPage;
        }
        public MainPageViewModel()
        {
            ChangeGroupId();
        }
        private List<CbGroup> CbGroupList = new List<CbGroup>();
        public List<CbGroup> CbGroupLists
        {
            get { return CbGroupList; }
            set { this.SetProperty(ref this.CbGroupList, value); }
        }
        private List<CbItem> CbItemList = new List<CbItem>();
        public List<CbItem> CbItemLists
        {
            get { return CbItemList; }
            set { this.SetProperty(ref this.CbItemList, value); }
        }

        private string txGroupId;
        public string TxGroupId
        {
            get { return txGroupId; }
            set
            {
                this.SetProperty(ref this.txGroupId, value);
                ChangeItemId(txGroupId);
            }
        }
        private string txItemId;
        public string TxItemId
        {
            get { return txItemId; }
            set { this.SetProperty(ref this.txItemId, value); }
        }
        private void ChangeGroupId()
        {
            CbGroupList.Add(new CbGroup("A", "Japan"));
            CbGroupList.Add(new CbGroup("B", "United States"));
            CbGroupList.Add(new CbGroup("C", "CANADA"));
        }
        private void ChangeItemId(string ValueId)
        {
            try
            {
                if (CbItemLists != null)
                {
                    CbItemList.Clear();
                }
                TxItemId = null;
                //
                switch (ValueId)
                {
                    case "A":
                        CbItemList.Add(new CbItem("A1", "Tokyo"));
                        break;
                    case "B":
                        CbItemList.Add(new CbItem("B1", "Chicago"));
                        CbItemList.Add(new CbItem("B2", "Dayton"));
                        break;
                    case "C":
                        CbItemList.Add(new CbItem("C1", "Toronto"));
                        CbItemList.Add(new CbItem("C2", "Halifax"));
                        CbItemList.Add(new CbItem("C3", "Edmonton"));
                        break;
                }
            }catch(Exception ex)
            {
                string stErrMessage = ex.Message;
            }
        }
    }
    public class CbGroup
    {
        public string GroupId { get; set; }
        public string GroupName { get; set; }
        public CbGroup(string ValueId, string ValueName)
        {
            GroupId = ValueId;
            GroupName = ValueName;
        }
    }
    public class CbItem
    {
        public string ItemId { get; set; }
        public string ItemName { get; set; }
        public CbItem(string ValueId, string ValueName)
        {
            ItemId = ValueId;
            ItemName = ValueName;
        }
    }
}
<Page
    x:Class="TestComboBox2.Views.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestComboBox2"
    xmlns:views="using:TestComboBox2.Views"
    xmlns:viewmodels="using:TestComboBox2.ViewModels"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Page.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Style.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Page.Resources>
    <Grid>
        <StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Style="{StaticResource TextBlckFontSize}"
                       Text="Country"
                           Foreground="Black"
                       />
                <ComboBox Style="{StaticResource ComboBoxStype}"
                      ItemsSource="{Binding CbGroupLists,Mode=OneWay}"
                      SelectedValue="{Binding TxGroupId,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                      SelectedValuePath="GroupId"
                      >
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock>
                            <Run Text="{Binding GroupName,Mode=OneWay}"/>
                            </TextBlock>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
            </StackPanel>
            <StackPanel Orientation="Horizontal">
                <TextBlock Style="{StaticResource TextBlckFontSize}"
                           Foreground="Black"
                       Text="City"
                       />
                <ComboBox Style="{StaticResource ComboBoxStype}"
                      ItemsSource="{Binding CbItemLists,Mode=OneWay}"
                      SelectedValue="{Binding TxItemId,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
                      SelectedValuePath="ItemId"
                      >
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock>
                            <Run Text="{Binding ItemName,Mode=OneWay}"/>
                            </TextBlock>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>

            </StackPanel>
        </StackPanel>
    </Grid>
</Page>

第二个组合框显示在第一个组合框中选择的国家的城市。

2 个答案:

答案 0 :(得分:0)

  

条件组合框无法正常工作

请将Object.prototype.values的类型更改为ObservableCollection,该类型表示一个动态数据集合,当添加,删除或刷新整个列表时提供通知。它将起作用。

CbItemList

答案 1 :(得分:0)

那么,您应该使用ObservableCollection而不是List,因为ObservableCollection是一个类型集合,当添加或删除项目时会提供通知。 ObservableCollection类具有INotifyPropertyChanged的自己的实现。因此,当此集合进行一些更改时,它将反映或通知UI。对代码进行一些修改

private ObservableCollection<CbItem> CbItemList 
public ObservableCollection<CbItem> CbItemLists
{
    get { return CbItemList ?? (CbItemList = new ObservableCollection<CbItem>()); }
    set { this.SetProperty(ref this.CbItemList, value); }
}