2011-08-09 12 views
1

コンボボックスがビュー内にあり、正常に動作しますが、アイテムソースのモデルの別のフィールドをバインドに使用する(または私が使用したときに)別のフィールドを更新したいフィールドスコア。 など。コンボボックスでルール1を選択すると、ビューのスコアフィールドが1で更新されます。選択したアイテムをルール2に変更すると、スコアフィールドに2が表示されます。EventToCommand SelectionChangedコンボボックスでパラメータを渡す

私のコードは、私が所望の結果を達成するために「ここに途中で」実験ので、私はitemsourceがスコアでデータグリッドとScoreViewを持って少し不自由かもしれません:

SelectionChangedCommand = new RelayCommand<string>((_score) => SelectedRuleChanged(_score)); 

private void SelectedRuleChanged(string _score) 
    { 
     int _tmpint; 
     _tmpint = this.SelectedScore.Model.score; 
     int.TryParse(_score, out _tmpint); 
     this.SelectedScore.Model.score = _tmpint; 

     //Todo weghalen lokale rules collectie en get_rules voids 
     //get_rules_by_ruleset(this.SelectedMatch.Model.ruleset); 

    } 
<ComboBox 
Height="23" HorizontalAlignment="Left" 
Name="cmbRules" VerticalAlignment="Top" Width="100" ItemsSource="{Binding MatchVM.Rules}"  SelectedValue="{Binding Model.ruleid, Mode=TwoWay}" 
DisplayMemberPath="Model.name" SelectedValuePath="Model.ruleid"> 
<i:Interaction.Triggers> 
    <i:EventTrigger EventName="SelectionChanged"> 
     <cmd:EventToCommand Command="{Binding MainScore.SelectionChangedCommand, Mode=OneWay,  
     Source={StaticResource Locator}}" CommandParameter="{Binding Model.score}"/> 
    </i:EventTrigger> 
    </i:Interaction.Triggers> 

私はselecteditem要素(これは私がmodel.scoreにバインドしたもの)をコマンドパラメータとして渡そうとします。 MatchVMRuleのselecteditemを使用するべきですが、スコアテキストフィールドがルールViewModelの代わりにスコアViewModelにバインドされるのでしょうか?事前に

おかげで、私は最終的に二つの別々の小道具を作成することによって、それを解決し を解決し

マイク

UPDATEは私のMainScoreViewModelに収集し、選択されたルールをルール。 私はeventocommandを取り除くとSelectedRuleのセッターでのルールのモデルのスコアに基づいて、私のスコアモデルにスコアフィールドの更新を処理しました:

/// <summary> 
    /// Gets the SelectedRule property. 
    /// Changes to that property's value raise the PropertyChanged event. 
    /// </summary> 
    public RuleViewModel SelectedRule 
    { 
     get 
     { 
      return _selectedRule; 
     } 

     set 
     { 
      if (_selectedRule == value) 
      { 
       return; 
      } 
      _selectedRule = value; 
      this.SelectedScore.Model.score = this.SelectedRule.Model.score; 
      RaisePropertyChanged(SelectedRulePropertyName); 
     } 
    } 



<ComboBox 
Height="23" HorizontalAlignment="Left" 
Name="cmbRules" VerticalAlignment="Top" Width="100" ItemsSource="{Binding ScoreVM.Rules,  
Mode=TwoWay}" SelectedValue="{Binding Model.ruleid, Mode=TwoWay}" 
DisplayMemberPath="Model.name" SelectedValuePath="Model.ruleid" SelectedItem="{Binding 
ScoreVM.SelectedRule, Mode=TwoWay}"> 
</ComboBox> 

答えて

2

何あなたのViewModelにPropertyChange通知の使用について表示から処理する代わりにSelectionChangedイベントを処理するにはどうすればよいですか?

public ParentViewModel() 
{ 
    this.Model.PropertyChanged += Model_PropertyChanged; 
} 

void Model_PropertyChanged(object sender, PropertyChangedEventArgs e) 
{ 
    switch (e.PropertyName) 
    { 
     case "ruleid": 
      this.SelectedScore.Model.Score = Model.Score; 
    } 
} 
+0

私はmodel.scoreがScoreViewModel.Model.score代わりのRuleViewModel.Model.score ... –

+0

だと思い感謝レイチェルが、Model.Score = 0そう私はあなたが何をしようとしていたかを正確にわからないんだけどあなたのコードがちょっと混乱しているとわかりました。 – Rachel

+0

あなたはそうです。問題は私のViewModelsにPHP Webサービスを使用していることです。それは私のためにそれを少し混乱させる。 –

関連する問題