EventToCommand SelectionChanged 在 Combobox 中传递参数

发布于 2024-11-28 17:34:51 字数 2616 浏览 0 评论 0原文

我有一个在视图中包含规则的组合框,它运行良好,但我希望使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

聽兲甴掵 2024-12-05 17:34:51

如何使用 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;
    }
}

What about using the PropertyChange notification on your ViewModel to handle the SelectionChanged event instead of trying to handle it from the View?

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文