代码之家  ›  专栏  ›  技术社区  ›  eye_am_groot

自定义类设置程序未更新

  •  0
  • eye_am_groot  · 技术社区  · 6 年前

    我有课, KeyCombos 包含 Modifier Keys Keys

    private ModifierKeys _modifierKeys;
    private Key _key;
    
    public ModifierKeys ModifierKeys
    {
        get { return _modifierKeys; }
        set
        {
            if (_modifierKeys == value)
                return;
    
            _modifierKeys = value;
            RaisePropertyChanged(nameof(ModifierKeys));
        }
    }
    
    public Key Key
    {
        get { return _key; }
        set
        {
            if (_key == value)
                return;
    
            _key = value;
            RaisePropertyChanged(nameof(Key));
        }
    }
    
    public KeyCombo(ModifierKeys modifierKeys, Key key)
    {
        Key = key;
        ModifierKeys = modifierKeys;
    }
    

    哪里 RaisePropertyChanged INotifyPropertyChanged .

    在我的 ViewModel ,我有绑定到各种组合键的属性,例如:

    private KeyCombo _firstKeyCombo;
    public KeyCombo FirstKeyCombo
    {
        get { return _firstKeyCombo; }
        set
        {
            if (_firstKeyCombo == value)
                return;
    
            _firstKeyCombo = value;
            // Validation method called here
            RaisePropertyChanged(nameof(FirstKeyCombo));
        }
    }
    

    每一部分 KeyCombo 设置在 视图模型 ,即:

    FirstKeyCombo = new KeyCombo(ModifierKeys.Alt, Key.T);
    

    public IEnumerable<Key> Keys => _availableKeys;
    ObservableCollection<Key> _availableKey; // set to all available keys
    public IEnumerable<ModifierKeys> Modifiers => _modifierKeys;
    ObservableCollection<ModifierKeys> _modifierKeys;  // set to all modifier keys
    

    然后被捆绑在 View ComboBoxs :

    <StackPanel Orientation="Horizontal" Grid.Row="13" Grid.Column="2">
        <ComboBox ItemsSource="Keys" 
                  SelectedItem="{Binding FirstKeyCombo.Key, 
                           Mode=TwoWay, 
                           UpdateSourceTrigger=PropertyChanged}"/>
        <ComboBox ItemsSource="Modifiers" 
                  SelectedItem="{Binding FirstKeyCombo.ModifierKey, 
                           Mode=TwoWay, 
                           UpdateSourceTrigger=PropertyChanged}"/>
    </StackPanel>
    

    这个很好,合适的 Key ModifierKeys 正确显示。我需要更新 FirstKeyCombo 钥匙 ModiferKeys 更改(验证结束) 按键组合 InotifyProperty已更改 修改键 钥匙 )通过 Combobox 第一键组合

    1 回复  |  直到 6 年前
        1
  •  2
  •   Andreas    6 年前

    因此,在创建FirstKeyCombo之后,添加PropertyChangedEventhandler

    FirstKeyCombo.PropertyChanged += OnFirstKeyComboPropertyChanged;
    

    应该是这样的:

    private void OnFirstKeyComboPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
      //remove the handler
      FirstKeyCombo.PropertyChanged -= OnFirstKeyComboPropertyChanged;
      //create new object because of referential equlity check in WPF
      FirstKeyCombo = new KeyCombo(FirstKeyCombo.ModifierKeys, FirstKeyCombo.Key);
      //add the handler to the new object
      FirstKeyCombo.PropertyChanged += OnFirstKeyComboPropertyChanged;
    }
    

    RaisePropertyChanged(nameof(FirstKeyCombo));
    

    至于评论,这就是反思的方式。KeyCombo.名称必须是属性的名称。

    private void OnFirstKeyComboPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
      KeyCombo combo = sender as KeyCombo;
      //remove the handler
      combo.PropertyChanged -= OnFirstKeyComboPropertyChanged;
      //create new object because of referential equlity check in WPF
      combo = new KeyCombo(combo.ModifierKeys, combo.Key, combo.Name);
      //add the handler to the new object
      combo.PropertyChanged += OnFirstKeyComboPropertyChanged;
      //Get the ViewModel-Type
      Type t = ViewModel.GetType();
      //Get the property with the name
      PropertyInfo pi = t.GetProperty(combo.Name);
      //set the value of the property
      pi.SetValue(ViewModel,combo);
    }
    

    或者用字典。也KeyCombo.名称必须是属性的名称。

    private void OnFirstKeyComboPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
      KeyCombo combo = sender as KeyCombo;
      //remove the handler
      combo.PropertyChanged -= OnFirstKeyComboPropertyChanged;
      //create new object because of referential equlity check in WPF
      combo = new KeyCombo(combo.ModifierKeys, combo.Key, combo.Name);
      //add the handler to the new object
      combo.PropertyChanged += OnFirstKeyComboPropertyChanged;
      dictionaryWithAllCombos[combo.Name] = combo;
      RaisePropertyChanged(combo.Name);
    }
    

    在ViewModel中

    public FirstKeyCombo FirstKeyCombo
    {
      get { return dictionary["FirstKeyCombo"]; }
      set 
      { 
        dictionaryWithAllCombos["FirstKeyCombo"] = value; 
        RaisePropertyChanged(nameof(FirstKeyCombo));
      }
    }