【问题标题】:WPF make combobox binding twowaysWPF使组合框双向绑定
【发布时间】:2023-03-14 14:57:01
【问题描述】:

我有一个可编辑的组合框,并且我有一个按钮,当绑定到组合框的 SelectedReplacement 不为空时启用并在它为空时禁用。当它为空时,我会输入一些随机文本以使按钮启用,问题是当我输入文本时它不会启用。制作 Mode TwoWay 并没有帮助。我假设设置 propertychangedevent 会将新文本绑定到 SelectedReplacement,但我错了,因此不胜感激。

<ComboBox ItemsSource="{Binding SelectedError.Suggestions}"
                      Text="{m:Binding Path=SelectedError.SelectedReplacement, Mode=TwoWay}"
                      IsEditable="True"
                      HorizontalAlignment="Stretch"/>

我也试图改变属性

    private void ViewModelPropertyChanged(SpellcheckViewModel sender, PropertyChangedEventArgs e)
    {
        if (e.PropertyName == nameof(sender.SelectedError.SelectedReplacement))
        {
            _correctCommand?.Refresh();
        }
    }

【问题讨论】:

    标签: wpf xaml combobox binding


    【解决方案1】:

    我尝试编写一个演示项目来满足您的要求。

    主要是,启用状态由视图模型中的另一个布尔属性IsButtonEnabled 控制,该属性的值由InputText 属性控制,该属性由您在ComboBox 控件中输入的文本控制。

    这是用户界面:

    <StackPanel Margin="10">
        <ComboBox
            x:Name="cmb"
            IsEditable="True"
            ItemsSource="{Binding AllItems}"
            TextBoxBase.TextChanged="cmb_TextChanged"
            TextSearch.TextPath="Name">
    
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}" />
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
        <TextBox
            x:Name="hiddenTextBox"
            Text="{Binding InputText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
            Visibility="Collapsed" />
        <Button
            x:Name="btn"
            Margin="0,10,0,0"
            Content="Show message"
            IsEnabled="{Binding IsButtonEnabled}" />
    </StackPanel>
    

    这是视图模型中的主要逻辑:

        public ObservableCollection<Item> AllItems
        {
            get { return _allItems; }
            set { _allItems = value; this.RaisePropertyChanged("AllItems"); }
        }
    
        public bool IsButtonEnabled
        {
            get { return _isButtonEnabled; }
            set { _isButtonEnabled = value; this.RaisePropertyChanged("IsButtonEnabled"); }
        }
    
        /// <summary>
        /// When InputValue changed, change the enable state of the button based on the current conditions
        /// </summary>
        public string InputText
        {
            get { return _inputText; }
            set
            {
                _inputText = value;
                this.RaisePropertyChanged("InputText");
    
                // You can control the enable state of the button easily
                if (AllItems.Any(item => item.Name == value))
                {
                    // SelectedItem is not null
                    IsButtonEnabled = true;
                }
                else if (!string.IsNullOrEmpty(value))
                {
                    // SelectedItem is null
                    IsButtonEnabled = true;
                }
                else
                {
                    IsButtonEnabled = false;
                }
            }
        }
    

    最后,这里是项目:ComboBoxDemo

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-14
      • 2012-06-18
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多