EventToCommand SelectionChanged 在 Combobox 中传递参数
我有一个在视图中包含规则的组合框,它运行良好,但我希望使用 itemsource 中模型的另一个字段来绑定(或在我使用它时)更新另一个字段分数。 例如,如果您在组合框中选择规则 1,它应该将视图中的分数字段更新为 1,如果您将所选项目更改为规则 2,它应该在分数字段中显示 2。
我的代码可能有点残缺,因为我在“途中”进行了实验以达到所需的结果,我有一个带有数据网格的 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 的选定项,但分数文本字段绑定到分数 ViewModel 而不是规则 ViewModel?
提前致谢,
迈克
更新已解决 我最终通过在 MainScoreViewModel 中创建两个单独的 props 规则集合和选定的规则来解决这个问题。 我摆脱了 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>
I have a combobox with rules in a view and it works well, but I want another field of the model from the itemsource to be used to bind to (or as I used it) update another field score.
E.g. if you select rule 1 in the combobox it should update the score field in the view with 1 and if you change the selecteditem to rule 2 it should show 2 in the score field.
My code might be a bit crippled because I experimented 'on the way here' to achieve the desired result, I have a ScoreView with a datagrid which itemsource is scores:
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>
I try to pass the elements selecteditem (which I bound to model.score) as a command parameter..
Maybe I should use the selecteditem of MatchVMRule but the score text field is bound to the Scores ViewModel instead of the Rules ViewModel?
Thanks in advance,
Mike
UPDATE SOLVED
I finally solved it by creating two separate props rules collection and selected rule in my MainScoreViewModel.
I got rid of the eventocommand and handled the update of the score field in my Score Model based on the score of the Rule Model in the setter of the 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如何使用 ViewModel 上的
PropertyChange
通知来处理SelectionChanged
事件,而不是尝试从视图中处理它?What about using the
PropertyChange
notification on your ViewModel to handle theSelectionChanged
event instead of trying to handle it from the View?