2016-11-13 55 views
0

我與項目A,B,C,d組合框,E.如何改變組合框的SelectedValue編程方式與MVVM

如何更改組合框的的SelectedValue用戶選擇後,因此,如果用戶從選擇列表項「A」,SelectedValue將是「D」(就像他自己選擇了D一樣)。

的XAML:

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

視圖模型:

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; 

} 

感謝

回答

2

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

比當SelectedItemChanged像

<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 })); 
} 
} 
+0

能否請您添加UpdateNodeCategoryCommand語法,你可以調用命令,我仍然是複雜的這一點。 – dov

+0

我現在在外面。您可以檢查此https://msdn.microsoft.com/en-us/magazine/dn237302.aspx有命令參數 – Miguel

+0

如果您使用MVVM庫它應該有ICommand實現。如果你不是,你可以使用下面的一個http://stackoverflow.com/questions/5069783/which-mvvm-framework-is-the-best-framework-for-wpf-apps。 MvvmLight有RelayCommand'私人RelayCommand _updateNodeCategoryCommand; 公共RelayCommand UpdateNodeCategoryCommand { 得到 { 回報_updateNodeCategoryCommand ? (_updateNodeCategoryCommand = new RelayCommand ( nodeCategory => { NodeCategory = nodeCategory })); } }' – Miguel

相關問題