我在视图中有一个带有规则的组合框并且它运行良好,但是我想要使用来自itemsource的模型的另一个字段来绑定(或者当我使用它时)更新另一个字段分数。 例如。如果你在组合框中选择规则1,它应该用1更新视图中的得分字段,如果你将selecteditem更改为规则2,它应该在得分字段中显示2。
我的代码可能有点瘫痪,因为我在这里的路上进行了实验'为了达到预期的效果,我有一个带有datagrid的ScoreView,其中itemsource是得分:
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但是得分文本字段绑定到Scores ViewModel而不是Rules ViewModel?
提前致谢,
麦克
更新已解决 我最终通过在MainScoreViewModel中创建两个单独的道具规则集合和选定规则来解决它。 我摆脱了eventocommand并根据SelectedRule的setter中规则模型的得分处理了我的得分模型中得分字段的更新:
/// <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>
答案 0 :(得分:2)
如何在ViewModel上使用PropertyChange
通知来处理SelectionChanged
事件,而不是尝试从View中处理它?</ p>
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;
}
}