简体   繁体   English

WPF:从自定义控件引用窗口

[英]WPF: Referencing the Window from a Custom Control

I have followed the Accepted Answer's instructions from this post as regards creating a code behind file for a Resource Dictionary, and it worked...so now I can attach events to controls in the generic.xml file. 我已按照这篇文章中关于“接受答案”的说明进行操作,关于为资源字典在文件后面创建代码,它的工作原理是……所以现在我可以将事件附加到generic.xml文件中的控件上。

But now I want to be able to call the DragMove() method from an event in there and since there aren't any references to the window hosting the dictionary at the time, I don't know how to call this DragMove() method. 但是现在我希望能够从那里的事件中调用DragMove()方法,并且由于当时没有指向托管字典的窗口的任何引用,因此我不知道如何调用此DragMove()方法。

So, from a Resource Dictionary Code behind file, is there any way I can make a reference to the window that will currently be hosting that Resource Dictionary? 因此,从文件后面的资源字典代码中,有什么方法可以引用当前将托管该资源字典的窗口?


[Update] (Temporary Solution) [更新](临时解决方案)

As a simple (yet stupid) workaround, I have currently done the following: 作为一种简单的(但很愚蠢的)解决方法,我目前已执行以下操作:
Since I can reference the Application.Current.MainWindow from the Generic.xaml.cs code-behind, I now have this in the Generic.xaml.cs : 由于我可以从Generic.xaml.cs代码后面引用Application.Current.MainWindow ,因此现在可以在Generic.xaml.cs

private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{               
    Application.Current.MainWindow.DragMove();
}

And then I'm attaching PreviewMouseLeftButtonDown handler to each Window I have, like such: 然后,我将PreviewMouseLeftButtonDown处理程序附加到我拥有的每个Window上,例如:

private void Window_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    Application.Current.MainWindow = this;
}

It, well, it works...and until someone can come up with the proper way on how to do this, It should serve me well enough. 它很好,它起作用了……直到有人能找到正确的方法来做到这一点,它应该足以为我服务。

There is no way I know of doing this. 我没有办法做到这一点。 However, if you're trying to determine the Window given a specific resource, you could use a RelativeSource : 但是,如果您要确定给定特定资源的Window ,则可以使用RelativeSource

<SolidColorBrush x:Key="MyBrush" Color="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Converter={StaticResource WindowToColorConverter}"/>

And if you're doing it from code, you can use Window.GetWindow() . 而且,如果您通过代码执行此操作,则可以使用Window.GetWindow() You just need a DependencyObject hosted in that Window . 您只需要在该Window托管一个DependencyObject

From architectural point of view I would say you are about to break the paradigm. 从体系结构的角度来看,我会说您将打破这种范例。 It might be a bad decision providing Resource Dictionary with notion of UI that consumes it and giving some logic other than providing resources. 为资源字典提供使用它的UI概念并提供一些除提供资源外的逻辑可能是一个错误的决定。

You might want some Adapter between UI and resource dictionary, or Controller if this is really needed to wire Resource Dictionary but again you shouldn't inject any logic in a resource container... 您可能需要UI和资源字典之间的适配器,或者如果确实需要此适配器来连接资源字典,则可能需要控制器,但是同样,您不应该在资源容器中注入任何逻辑...

You can access your main window through 您可以通过以下方式访问主窗口

Application.Current.MainWindow

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM