【WPF】级联Combobox及其与ListView的联动

这篇博客介绍了如何在WPF应用中使用级联Combobox,并实现其与ListView的联动效果。采用MVVM框架,通过自定义Command处理交互。内容包括:使用两个Dictionary实现级联Combobox,为Combobox添加IsChecked属性以实现Checkbox功能,处理前端绑定和命令支持,解决Combobox选中问题,以及ListView中自定义控件的更新逻辑。此外,还详细阐述了射频标准变化时如何更新数据源和ListView记录,以及带宽切换对已测项目的响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 一个射频标准支持多种带宽,多个频点,级联Combobox用两个Dictionary实现

ListView第一列时一个自定义控件,图片根据标准切换,Checkbox默认null,标准下的带宽和频点全部测试完毕后改为 true

 采用了 MVVM框架,简单自定义一个Command

    internal class RelayCommand : ICommand
    {
        public event EventHandler CanExecuteChanged;

        public Action action;

        public RelayCommand(Action action)
        {
            this.action = action;
        }


        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            action?.Invoke();
        }
    }

    internal class RelayCommand<T> : ICommand
    {
        public event EventHandler CanExecuteChanged;

        public Action<T> action;


        public RelayCommand(Action<T> action)
        {
            this.action = action;
        }


        public bool CanExecute(object parameter)
        {
            return true;
        }

        public void Execute(object parameter)
        {
            action?.Invoke((T)parameter);
        }
    }

Combobox实体。中心频点要做成Checkbox,就加了个IsChecked属性单独封装了

    public class ComboboxSignal
    {
        public ComboboxSignal(string standard, List<string> bandWidthList, List<CenterFreq> centerFreqList)
        {
            Standard = standard;
            BandWidthList = bandWidthList;
            CenterFreqList = centerFreqList;
        }

        public string Standard { get; set; }
        public List<string> BandWidthList { get; set; } = new List<string>();
        public List<CenterFreq> CenterFreqList { get; set; } = new List<CenterFreq>();

    }

    public class CenterFreq : INotifyPropertyChanged
    {
        public event Action<CenterFreq> CheckedChanged;
        public event PropertyChangedEventHandler PropertyChanged;

        public CenterFreq(string freq, bool isChecked)
        {
            Freq = freq;
            IsChecked = isChecked;
        }

        private string freq = string.Empty;
        public string Freq { get { return freq; } set { freq = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Freq"));}
        }

        private bool isChecked = false;
        public bool IsChecked
        {
            get { return isChecked; }
            set
            {
                isChecked = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsChecked"));
                if (isChecked)
                {
                    CheckedChanged?.Invoke(this);
                }

            }
        }
    }

填充数据源,第一个频点默认选中

        private static List<ComboboxSignal> m_signalList = new List<ComboboxSignal>();//信号列表
        
        /// <summary>
        /// 信号标准
        /// </summary>
        public List<string> StandardList { get; set; } = new List<string> { "802.11a/g", "802.11b", "802.11n", "802.11ac" };

       private void InitSignals()
        {
            List<CenterFreq> centerFreqList0 = new List<CenterFreq>();
            centerFreqList0.Add(new CenterFreq("36/5180M/Hz", true));
            centerFreqList0.Add(new CenterFreq("40/5200M/Hz", false));
            centerFreqList0.Add(new CenterFreq("44/5220M/Hz", false));

            List<CenterFreq> centerFreqList1 = new List<CenterFreq>();
            centerFreqList1.Add(new CenterFreq("1/2412M/Hz", true));
            centerFreqList1.Add(new CenterFreq("2/2417M/Hz", false));
            centerFreqList1.Add(new CenterFreq("3/2422M/Hz", false));

            List<CenterFreq> centerFreqList2 = new List<CenterFreq>();
            centerFreqList2.Add(new CenterFreq("36/5180M/Hz", true));
            centerFreqList2.Add(new CenterFreq("40/5200M/Hz", false));
            centerFreqList2.Add(new CenterFreq("44/5220M/Hz", false));

            List<CenterFreq> centerFreqList3 = new List<CenterFreq>();
            centerFreqList3.Add(new CenterFreq("1/2412M/Hz", true));
            centerFreqList3.Add(new CenterFreq("2/2417M/Hz", false));
            centerFreqList3.Add(new CenterFreq("3/2422M/Hz", false));

            m_signalList.Add(new ComboboxSignal(StandardList[0], new List<string> { "20M" }, centerFreqList0));
            m_signalList.Add(new ComboboxSignal(StandardList[1], new List<string> { "20M" }, centerFreqList1));
            m_signalList.Add(new ComboboxSignal(StandardList[2], new List<string> { "20M", "40M" }, centerFreqList2));
            m_signalList.Add(new ComboboxSignal(StandardList[3], new List<string> { "20M", "40M", "80M", "160M" }, centerFreqList3));

            CurrentSignal = m_signalList[0];

            ListViewSignalCollection.Add(new ListViewSignal(CurrentSignal));
        }

前端绑定,下载Interactivity库,为Combobox提供命令支持

<Grid>
            <StackPanel HorizontalAlignment="Left" Margin="50">
                <TextBlock Margin="0 10 0 0" Text="{DynamicResource txtLanguage}" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <TextBlock Margin="0 25 0 0" Text="{DynamicResource txtStandard}" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <TextBlock Margin="0 25 0 0" Text="{DynamicResource txtBandWidth}" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                <TextBlock Margin="0 25 0 0" Text="{DynamicResource txtCenterFreq}" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/>
            </StackPanel>
            <StackPanel HorizontalAlignment="Center" Margin="0 50 0 0">
                <ComboBox SelectedIndex="0" FontSize="20" Height="50" Width="200" Margin="0 0 0 10">
                    <i:Interaction.Triggers>
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值