在 SelectionChanged 事件上使用交互触发器的正确​​方法

发布于 2024-10-29 02:35:13 字数 518 浏览 4 评论 0原文

我有一个命令连接到该事件,以便它确实触发,但我在 CommandParameter 中得到的是先前选定的项目,或者可能是 SelectionChanged 完成之前选定的项目。

无论哪种方式,都不知道要更改什么才能从事件中获取新选择的项目。

<i:Interaction.Triggers>
  <i:EventTrigger EventName="SelectionChanged">
    <cmd:EventToCommand 
    Command="{Binding Main.SelectedRecordCommand, Source={StaticResource Locator}}" 
    CommandParameter="{Binding SelectedItem, ElementName=listBillingRecords}" 
    />
   </i:EventTrigger>
</i:Interaction.Triggers>

谢谢

I have a command wired to the event such that it does fire, but what I get in the CommandParameter is the previously selected item, or maybe it's the selected item before the SelectionChanged completes.

Either way, not sure what to change to get the newly selected item from the event.

<i:Interaction.Triggers>
  <i:EventTrigger EventName="SelectionChanged">
    <cmd:EventToCommand 
    Command="{Binding Main.SelectedRecordCommand, Source={StaticResource Locator}}" 
    CommandParameter="{Binding SelectedItem, ElementName=listBillingRecords}" 
    />
   </i:EventTrigger>
</i:Interaction.Triggers>

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

落在眉间の轻吻 2024-11-05 02:35:13

值得使用触发器吗?如果集合的 XAML 元素(列表框、网格等)绑定到在视图模型上公开集合的属性,则可以利用数据绑定和内置 MVVM Light 信使来通知您属性更改以更 MVVM 友好的方式处理新旧值。这个例子不一定是WP7特定的,但我认为它的工作原理是一样的。

例如,这可能是数据绑定集合:

    public const string BillingRecordResultsPropertyName = "BillingRecordResults";
    private ObservableCollection<BillingRecord> _billingRecordResults = null;
    public ObservableCollection<BillingRecord> BillingRecordResults
    {
        get
        {
            return _billingRecordResults;
        }

        set
        {
            if (_billingRecordResults == value)
            {
                return;
            }

            var oldValue = _billingRecordResults;
            _billingRecordResults = value;

            // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
            RaisePropertyChanged(BillingRecordResultsPropertyName, oldValue, value, true);
        }
    }

我喜欢在 ViewModel 上公开一个属性,该属性是我公开的任何集合的“选定项”。因此,对于 ViewModel,我将使用 MVVMINPC 代码段添加此属性:

    public const string SelectedBillingRecordPropertyName = "SelectedBillingRecord";
    private BillingRecord _selectedBillingRecord = null;
    public BillingRecord SelectedBillingRecord
    {
        get
        {
            return _selectedBillingRecord;
        }

        set
        {
            if (_selectedBillingRecord == value)
            {
                return;
            }

            var oldValue = _selectedBillingRecord;
            _selectedBillingRecord = value;

            // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
            RaisePropertyChanged(SelectedBillingRecordPropertyName, oldValue, value, true);
        }
    }

现在,如果将 XAML 元素的 SelectedItem 绑定到此公开属性,则当通过数据绑定在视图中选择时,它将填充该属性。

但更好的是,当您利用片段 MVVMINPC 时,您可以选择是否向任何收听的人广播结果。在本例中,我们想知道 SelectedBillingRecord 属性何时发生变化。因此,您可以在 ViewModel 的构造函数中包含此内容:

Messenger.Default.Register<PropertyChangedMessage<BillingRecord>>(this, br => SelectedRecordChanged(br.NewValue));

在 ViewModel 的其他位置,无论您希望发生什么操作:

    private void SelectedRecordChanged(BillingRecord br)
    {
        //Take some action here
    }

希望这会有所帮助...

Is it worth using a trigger? If whatever your XAML element is for the collection (listbox, grid, etc) is bound to a property exposing a collection on your viewmodel, you can leverage both databinding and the built-in MVVM Light messenger to notify you of a property change with both old and new values in a more MVVM-friendly way. This example isn't necessarily WP7-specific, but I think it would work the same.

For example, this might be the databound collection:

    public const string BillingRecordResultsPropertyName = "BillingRecordResults";
    private ObservableCollection<BillingRecord> _billingRecordResults = null;
    public ObservableCollection<BillingRecord> BillingRecordResults
    {
        get
        {
            return _billingRecordResults;
        }

        set
        {
            if (_billingRecordResults == value)
            {
                return;
            }

            var oldValue = _billingRecordResults;
            _billingRecordResults = value;

            // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
            RaisePropertyChanged(BillingRecordResultsPropertyName, oldValue, value, true);
        }
    }

I like to expose a property on my ViewModel that is a "selected item" of whatever collection I'm exposing. So, to the ViewModel, I would add this property using the MVVMINPC snippet:

    public const string SelectedBillingRecordPropertyName = "SelectedBillingRecord";
    private BillingRecord _selectedBillingRecord = null;
    public BillingRecord SelectedBillingRecord
    {
        get
        {
            return _selectedBillingRecord;
        }

        set
        {
            if (_selectedBillingRecord == value)
            {
                return;
            }

            var oldValue = _selectedBillingRecord;
            _selectedBillingRecord = value;

            // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
            RaisePropertyChanged(SelectedBillingRecordPropertyName, oldValue, value, true);
        }
    }

Now, if you bind the SelectedItem of the XAML element to this exposed property, it will populate when selected in the View via databinding.

But even better, when you leverage the snippet MVVMINPC, you get to choose whether or not to broadcast the results to anyone listening. In this case, we want to know when the SelectedBillingRecord property changes. So, you can have this in the constructor for your ViewModel:

Messenger.Default.Register<PropertyChangedMessage<BillingRecord>>(this, br => SelectedRecordChanged(br.NewValue));

And elsewhere in your ViewModel, whatever action you want to have happen:

    private void SelectedRecordChanged(BillingRecord br)
    {
        //Take some action here
    }

Hope this helps...

岁吢 2024-11-05 02:35:13

我看到了同样的问题,并发现 SelectedItem 是正确的实现。

I have seen the same issue and found that SelectedItem is the correct implementation.

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