【问题标题】:C# WPF Data Binding CheckBoxes based on ComboBox Selection in MVVMC# WPF 数据绑定复选框基于 MVVM 中的组合框选择
【发布时间】:2020-12-07 21:32:04
【问题描述】:

我目前正在学习 WPF 的 MVVM 设计模式,我想根据我的 ComboBox 的 SelectedItem 绑定我的 CheckBoxes,它是数据绑定到字典,其中键是我的设备序列号,值是 ViewModel那个设备。

理想情况下,我的 SelectedItem 应该是 IOBoardViewModel 的值。

<ComboBox Name="io_boards_list" 
          ItemsSource="{Binding IOBoards}" DisplayMemberPath="Key" SelectedValuePath="Value"
          HorizontalAlignment="Left" Margin="361,28,0,0" VerticalAlignment="Top" Width="657" Height="35"/>

这是我最不清楚的地方:如何在我的 IOBoard 模型中绑定这些布尔值。

<CheckBox IsChecked="{ Binding Value.io_board.s0 }" Content="Input 0" Canvas.Left="10" Canvas.Top="10" Height="15"/>
<CheckBox IsChecked="{ Binding Value.io_board.s1 }" Content="Input 1" Canvas.Left="10" Canvas.Top="36" Height="15"/>
<CheckBox IsChecked="{ Binding Value.io_board.s2 }" Content="Input 2" Canvas.Left="10" Canvas.Top="63" Height="15"/>

在我的 IOBoardViewModel 中,我指的是我保存布尔值的实际模型。

        public Dictionary<string, IIOBoardViewModel> IOBoards { get; private set; }

        public FullIOBoard io_board
        {
            get { return _io_board; }
        }

这是我的 IOBoard 模型中的一个 sn-p:

        public bool s0
        {
            get
            {
                return _s0;
            }
            set
            {
                _s0 = value;
                OnPropertyChanged("s0");
            }
        }

        public bool s1
        {
            get
            {
                return _s1;
            }
            set
            {
                _s1 = value;
                OnPropertyChanged("s1");
            }
        }

        public bool s2
        {
            get
            {
                return _s2;
            }
            set
            {
                _s2 = value;
                OnPropertyChanged("s2");
            }
        }

我想知道我是否对如何在 MVVM 模式中将数据绑定在一起有正确的想法,如果没有,我将非常感谢您的见解或建议。

更新: 我想到了一种方法,我可以在绑定 ComboBox 的 SelectedItem 时使用(这将是字典的识别序列号到我的 ViewModel 中的变量):

public string SelectedItem
        {
            get
            {
                return _SelectedItem;
            }
            set
            {
                _SelectedItem = value;
            }
        }

现在我相信我应该为 CheckBox 绑定我的 IsChecked 到

<CheckBox IsChecked="{ Binding IOBoards[SelectedItem].s0 }" Content="Input 0" Canvas.Left="10" Canvas.Top="10" Height="15"/>

但是,即使我已将其中一个信号硬编码为始终为真,我仍然无法更新复选框:

_io_board.s0 = true;
_io_board.s1 = (inputSignal & 0x02) == 0x02;

【问题讨论】:

  • 你真的需要字典吗?添加 IO 板时会发生什么?将序列号添加到您的 IO 板。使用 SelectedItem 绑定而不是 selectedvaluepath。然后您可以将复选框绑定到您的视图模型中的 SelectedItem,例如 {Binding SelectedIOBoard.s0} 等等。

标签: c# wpf mvvm data-binding


【解决方案1】:

你可以直接绑定到ComboBox的SelectedItem,通过名字引用它:

<CheckBox IsChecked="{ Binding SelectedItem.Value.io_board.s0, ElementName=io_boards_list }" 

【讨论】:

  • 难以置信!这正是我想要的。昨天我以一种非常低效的方式解决了这个问题,但是您的解决方案使我的 CPU 使用率从 18-20% 提高到 1-3%。
猜你喜欢
  • 2011-02-21
  • 2018-10-19
  • 2015-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-03
  • 2017-06-20
  • 1970-01-01
相关资源
最近更新 更多