c# - 如何使用MVVM以编程方式更改ComboBox SelectedValue

标签 c# wpf mvvm combobox selectedvalue

我有一个带有项目A,B,C,D,E的ComboBox。

用户选择后如何更改组合框的SelectedValue,因此,如果用户从列表项“A”中选择,则SelectedValue将为“D”(就像他自己选择了D)。

Xml:

<StackPanel Orientation="Horizontal">
    <TextBox Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Height="25" Width="100" />
    <ComboBox 
        IsDropDownOpen="{Binding IsDropDownOpen, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
        ItemsSource="{Binding OffsetValues}"
        SelectedValue="{Binding NodeCategory, Mode=TwoWay}" 

        Height="25" Width="100" IsHitTestVisible="False" Background="AliceBlue">
        <ComboBox.Resources>
            <sys:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">0</sys:Double>
        </ComboBox.Resources> 
    </ComboBox>
</StackPanel>

ViewModel:
class ViewModel : ViewModelBase
{

    private IList<string> offsetValues =  new List<string>() { "mV", "V" };
    public IList<string> OffsetValues
    {
        get
        {
            return offsetValues;
        }
        set
        {
            offsetValues = value;
        }
    }


    private bool isDropDownOpen;

    public bool IsDropDownOpen
    {
        get { return isDropDownOpen; }
        set
        {
            isDropDownOpen = value;
            OnPropertyChanged();
        }
    }

    private string _name;
    public string Name
    {
        get { return _name; }
        set
        { 
            _name = value;
            OnPropertyChanged( "Name" );

            if( _name != "" )
            {
                isDropDownOpen = true;
                OnPropertyChanged( "IsDropDownOpen" );
            }

        }
    }

    private string _NodeCategory;
    public string NodeCategory
    {
        get
        {
            return _NodeCategory;
        }
        set
        {
            if( Convert.ToDouble( _name ) > 1000 )
            {
                _name = "1.0";

                OnPropertyChanged( "Name" );

                _NodeCategory = OffsetValues[1];

                OnPropertyChanged( "NodeCategory" );

            }
            else
            { 
                _NodeCategory = value;
                OnPropertyChanged( "NodeCategory" ); 
            } 
        }
    } 
}


public class ViewModelBase : INotifyPropertyChanged
{

    protected virtual void OnPropertyChanged( [CallerMemberName]string propertyName = null )
    {
        PropertyChanged.Invoke( this, new PropertyChangedEventArgs( propertyName ) );
    }

    public event PropertyChangedEventHandler PropertyChanged;

}

谢谢

最佳答案

添加xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
比当SelectedItemChanged之类的时候可以调用Command

    <ComboBox x:Name="NodeCategoriesCombobox">

<i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <i:InvokeCommandAction Command="{Binding UpdateNodeCategoryCommand}" CommandParameter="{Binding ElementName=NodeCategoriesCombobox, Path=SelectedValue}"/>
            </i:EventTrigger>
         </i:Interaction.Triggers>
</ComboBox>

比您添加UpdateNodeCategoryCommand并更新NodeCategory属性
private RelayCommand<string> _updateNodeCategoryCommand ;
 public RelayCommand<string> UpdateNodeCategoryCommand { 
get { return _updateNodeCategoryCommand ?? (_updateNodeCategoryCommand = new RelayCommand<string>( nodeCategory => { NodeCategory=nodeCategory })); 
}
 }

关于c# - 如何使用MVVM以编程方式更改ComboBox SelectedValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40571478/

相关文章:

c# - WPF MVVM 登录凭据

c# - MVVM 命令选择最后添加的项目

wpf - 如何设置 UIElement 的 DesiredSize.Width?

c# - 更改 Repeater Button onClick 的背景颜色

c# - 如何向 ListView 中的 DataTemplate 添加其他元素?

c# - 用于车牌识别系统的 Java 或 C#?

c - WPF 中平滑触摸滚动的 Storyboard

c# - 使屏幕阅读器可以访问 WPF 应用程序

c# - 使用集合时如何在MVVM中同步数据

c# - 将 C# 代码转换为 R 以将表转换为向量