Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用MVVM以编程方式更改组合框SelectedValue_C#_Wpf_Mvvm_Combobox_Selectedvalue - Fatal编程技术网

C# 如何使用MVVM以编程方式更改组合框SelectedValue

C# 如何使用MVVM以编程方式更改组合框SelectedValue,c#,wpf,mvvm,combobox,selectedvalue,C#,Wpf,Mvvm,Combobox,Selectedvalue,我有一个包含a、B、C、D、E项的组合框 如何在用户选择后更改组合框的SelectedValue,以便用户从列表项“a”中选择时,SelectedValue将为“D”(就像他自己选择了D一样) Xaml: 0 视图模型: class ViewModel : ViewModelBase { private IList<string> offsetValues = new List<string>() { "mV", "V" }; public I

我有一个包含a、B、C、D、E项的组合框

如何在用户选择后更改组合框的SelectedValue,以便用户从列表项“a”中选择时,SelectedValue将为“D”(就像他自己选择了D一样)

Xaml:


0

视图模型:

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;

}
类ViewModel:ViewModelBase
{
私有IList offsetValues=new List(){“mV”,“V”};
公共IList抵销价值
{
得到
{
返回偏移值;
}
设置
{
偏移值=值;
}
}
私有布尔是向下开放的;
公共图书馆是向下开放的
{
获取{return isDropDownOpen;}
设置
{
isDropDownOpen=值;
OnPropertyChanged();
}
}
私有字符串\u名称;
公共字符串名
{
获取{return\u name;}
设置
{ 
_名称=值;
不动产变更(“名称”);
如果(_name!=“”)
{
isDropDownOpen=true;
OnPropertyChanged(“IsDropDownOpen”);
}
}
}
私有字符串_NodeCategory;
公共字符串节点范畴
{
得到
{
返回_NodeCategory;
}
设置
{
if(Convert.ToDouble(_name)>1000)
{
_name=“1.0”;
不动产变更(“名称”);
_NodeCategory=偏移值[1];
不动产变更(“节点分类”);
}
其他的
{ 
_节点类别=值;
不动产变更(“节点分类”);
} 
}
} 
}
公共类ViewModelBase:INotifyPropertyChanged
{
受保护的虚拟void OnPropertyChanged([CallerMemberName]字符串propertyName=null)
{
调用(这是新的PropertyChangedEventArgs(propertyName));
}
公共事件属性更改事件处理程序属性更改;
}
谢谢

添加
xmlns:i=”http://schemas.microsoft.com/expression/2010/interactivity“

当选择EditEm时,您可以调用该命令,如

    <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>

然后添加UpdateNodeCategory命令和更新NodeCategory属性

private RelayCommand<string> _updateNodeCategoryCommand ;
 public RelayCommand<string> UpdateNodeCategoryCommand { 
get { return _updateNodeCategoryCommand ?? (_updateNodeCategoryCommand = new RelayCommand<string>( nodeCategory => { NodeCategory=nodeCategory })); 
}
 }
private RelayCommand\u updatenodecategory命令;
public RelayCommand UpdateNodeCategory命令{
获取{return{updateNodeCategoryCommand\({updateNodeCategoryCommand=newrelaycommand(nodeCategory=>{nodeCategory=nodeCategory}));
}
}
添加
xmlns:i=”http://schemas.microsoft.com/expression/2010/interactivity“

当选择EditEm时,您可以调用该命令,如

    <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>

然后添加UpdateNodeCategory命令和更新NodeCategory属性

private RelayCommand<string> _updateNodeCategoryCommand ;
 public RelayCommand<string> UpdateNodeCategoryCommand { 
get { return _updateNodeCategoryCommand ?? (_updateNodeCategoryCommand = new RelayCommand<string>( nodeCategory => { NodeCategory=nodeCategory })); 
}
 }
private RelayCommand\u updatenodecategory命令;
public RelayCommand UpdateNodeCategory命令{
获取{return{updateNodeCategoryCommand\({updateNodeCategoryCommand=newrelaycommand(nodeCategory=>{nodeCategory=nodeCategory}));
}
}

你能添加UpdateNodeCategoryCommand语法吗?我还是很复杂。我现在在外面。您可以使用参数检查这一点,如果您使用的是MVVM库,它应该具有ICommand实现。如果你不是,你可以使用以下方法之一。MvvmLight具有RelayCommand
private RelayCommand\u updatenodecategory命令;public RelayCommand UpdateNodeCategoryCommand{get{return{u UpdateNodeCategoryCommand=new RelayCommand(nodeCategory=>{nodeCategory=nodeCategory}));}
能否添加UpdateNodeCategoryCommand语法,我还是很复杂,我现在在外面。您可以使用参数检查这一点,如果您使用的是MVVM库,它应该具有ICommand实现。如果你不是,你可以使用以下方法之一。MvvmLight具有RelayCommand
private RelayCommand\u updatenodecategory命令;public RelayCommand UpdateNodeCategoryCommand{get{return{u UpdateNodeCategoryCommand=new RelayCommand(nodeCategory=>{nodeCategory=nodeCategory}));}