从资源字典中的控制模板访问类的方法
我想创建一个派生自 Window 但具有一些自定义功能的 WindowBaseClass。这样 WindowStyle 就不会了,我应用了自己的配色方案,并且还具有调整大小逻辑。
下面是 XAML 的片段,其中包含具有 MouseMOve 和 PreviewMouseDown 事件的“边框”之一。
<Rectangle Stroke="{x:Null}" x:Name="top" VerticalAlignment="Top" Height="5"
Grid.Column="1" Grid.Row="0" PreviewMouseDown="Resize"
MouseMove="DisplayResizeCursor">
<Rectangle.Fill>
<SolidColorBrush Color="{StaticResource Ocean}"/>
</Rectangle.Fill>
在我后面的代码中,我有调整大小、拖动等方法。当它全部包含在 Window1.xaml/.cs 中时,一切都工作得很好。
现在,我想使用我的配色方案创建一个自定义模板(例如在资源字典中),并且希望矩形中的 PreviewMouseDown 指向扩展 Window 的类中定义的方法。
能做到吗?任何帮助将不胜感激。
I want to make a WindowBaseClass that derives from Window but has a few custom functionalities. Such that WindowStyle would be none, I have my own color scheme applied and also have a resize logic.
Here is a snippet of the XAML that contains one of the 'borders' that has a MouseMOve and PreviewMouseDown events.
<Rectangle Stroke="{x:Null}" x:Name="top" VerticalAlignment="Top" Height="5"
Grid.Column="1" Grid.Row="0" PreviewMouseDown="Resize"
MouseMove="DisplayResizeCursor">
<Rectangle.Fill>
<SolidColorBrush Color="{StaticResource Ocean}"/>
</Rectangle.Fill>
In my code behind I have methods such as resize, drag etc. When it is all contained in a Window1.xaml/.cs it's all working nice.
Now I want to create a custom template (in a resource dictionary for example) with my color schemes and I want the PreviewMouseDown from the rectangle to point to the method defined in a class that extends Window.
Can it be done? Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为你会这样对待它。我认为您应该子类化 Window 并添加自定义 PreviewMouseDown 逻辑等,然后您将在资源字典中为新子类设置样式。
您可能还想研究自定义附加属性。
如果 PreviewMouseDown 等正在执行特定于一个窗口的逻辑而不是一般功能,那么无论哪种方式都可能不会有很好的效果。
I don't think you'd approach it quite like that. I think you'd subclass Window and add in your custom PreviewMouseDown logic, etc. and then you would set up the styles for your new subclass in the resource dictionary.
You might also want to look into a custom attached property.
If PreviewMouseDown, etc. are doing logic specific to one window as opposed to general functionality, this probably isn't going to work out so great either way.