WPF/C# 在 UserControl 中转发事件和 DataProvider 值

发布于 2024-09-25 07:44:57 字数 905 浏览 3 评论 0原文

我有一个包含 4 个 ToggleButton 的 UserControl,我想触发一个感兴趣的对象可以监听的自定义事件,该事件根据 ToggleButton Checked 值以及来自 DataContext 对象的值提供状态。

获取 ToggleButton 检查值并派生状态非常简单,但我无法弄清楚如何在 C# 代码隐藏中访问 DataContext 对象。

例如,如果感兴趣的对象从 UserControl 接收 RoutedEvent,我希望它能够访问 UserControl 的 DataContext 对象中的值。

我是否需要从 DataContext 对象公开特定属性,或者我可以以某种方式从 UserControl 的 API 公开 DataContext 对象吗?

更新。

为了更多地解释这个问题,我有一个项目列表,它在容器中创建一组 UserControl 实例,当每个项目添加到容器中时,我将事件侦听器附加到每个项目,并在其中一个 UserControls 是子项时发送事件单击/检查控件等。

获取对调度事件的 UserControl 的引用非常简单,但我无法访问 DataContext 对象,我是否需要分配一个公共属性来公开 DataContext 对象...

例如

private ControlViewModel myControlViewModel;
public ControlViewModel MyControlViewModel {
    get { return myControlViewModel; }
    set 
    {   
        this.DataContext = value;
        myControlViewModel = value;
    }
}

或者是有更好的办法吗?

任何提示将不胜感激,谢谢。

I have a UserControl which contains 4 ToggleButtons and I'd like to trigger a custom event that an interested object can listen for which provides a status, based on the ToggleButton Checked values and also value(s) from the DataContext object.

Getting the ToggleButton checked values and deriving a status is simple enough, however I can't work out how I access the DataContext object within the C# codebehind.

For example, if an interested object receives the RoutedEvent from the UserControl, I would like it to be able to access values from the UserControl's DataContext object.

Will I need to expose specific properties from the DataContext object or can I somehow expose the DataContext object from the UserControl's API?

Update.

To explain the problem a little more, I have a list of items which creates a set of UserControl instances in a container, I attach event listeners to each item as it's added to the container and send an event from one of the UserControls when it's child controls are clicked / checked etc.

Getting a reference to the UserControl that dispatched the event is straightforward enough, but I can't access the DataContext object, do I need to assign a public property to expose the DataContext object ...

e.g.

private ControlViewModel myControlViewModel;
public ControlViewModel MyControlViewModel {
    get { return myControlViewModel; }
    set 
    {   
        this.DataContext = value;
        myControlViewModel = value;
    }
}

or is there a better way?

Any tips would be appreciated, Thank you.

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

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

发布评论

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

评论(1

尤怨 2024-10-02 07:44:57

好吧,看起来我应该首先尝试最简单的解决方案...

...当然我可以像这样访问 DataContext 对象:

(userControl.DataContext as ControlViewModel).requiredProperty;

Update

所以我最终通过像这样的事件/委托对传递 DataContext 视图模型引用...

public delegate void StatusChangedHandler(string status, UserControlViewModel model);

public event StatusChangedHandler StatusChanged;

然后像这样调用事件...

StatusChanged.Invoke("message", DataContext as UserControlViewModel) 
// or DataContext as IUserControlModelInterface

这使我能够充分聚合来自 UserContol 子控件的事件,并从事件处理程序访问 DataContext 模型。

我仍然想知道是否有更好的最佳实践方法来做到这一点?

Well, it looks like I should've tried the simplest solution first...

...of course I can access the DataContext object like this:

(userControl.DataContext as ControlViewModel).requiredProperty;

Update

So I ended up passing the DataContext view model reference via a event/delegate pair like this...

public delegate void StatusChangedHandler(string status, UserControlViewModel model);

public event StatusChangedHandler StatusChanged;

And then just invoked the event like this...

StatusChanged.Invoke("message", DataContext as UserControlViewModel) 
// or DataContext as IUserControlModelInterface

Which allowed me to adequately aggregate events from the UserContol's child controls, and access the DataContext model from an event handler.

I still wonder if there is a more best practice way to do this?

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