C# 将方法绑定/引用到XAML WPF
我有这个xamlC# 将方法绑定/引用到XAML WPF,c#,wpf,xaml,events,binding,C#,Wpf,Xaml,Events,Binding,我有这个xaml <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:l="clr-namespace:My.Windows" &g
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:My.Windows"
>
<ObjectDataProvider x:Key="TitledWindow_Test" MethodName="Test" ObjectInstance={x:Type l:TitledWindow}">
<ControlTemplate x:Key="TitledWindowControlTemplateKey" x:Name="PART_ControlTemplate" TargetType="{x:Type l:TitledWindow}"
<Rectangle>
<Rectangle.Style>
<EventSetter Event="Mouse.MouseEnter" Handler="{StaticResource TitledWindow_Test}">
</Rectangle.Style>
</Rectangle>
</ControlTemplate>
</ResourceDictionary>
问题是我得到了以下错误:
错误1“ResourceDictionary”根元素需要一个x:Class属性来支持事件 XAML文件中的处理程序。移除MouseEnter事件的事件处理程序,
或者将x:Class属性添加到根元素。问题在于
模板
需要知道它所应用的对象是否有鼠标指针。不幸的是,即使将x:Type
应用于模板,xaml编译器也没有足够的资源继续运行
我以前也做过类似的事情,让ResourceDictionary
来识别我正在模板化的内容的潜力,看起来我使用了一种风格来绕过它。完整的代码
...
....
...
...
但是,您希望通过{StaticResource…}
将处理程序绑定到objectDataPresenter
上的方法,我不确定您是否可以。相反,您最好使用普通绑定{binding Path=…}
绑定到DataContext
,我认为您仍然可以通过{StaticResource..}提供DataContext
,您需要添加x:class属性并指定资源的位置,以及事件处理程序的位置。
请参阅以获取此示例。您可以通过将代码附加到您的ResourceDictionary中来实现这一点。实现这一目标的几个简单步骤是:
- 假设ResourceDictionary文件名为
CustomResources.xaml
。在ResourceDictionary之外的同一目录中添加另一个名为CustomResources.xaml.cs
的文件。创建从ResourceDictionary继承的部分类CustomResources
声明MouseEnter的处理程序,代码隐藏就绪
using System;
using System.Windows;
namespace WpfApplication1
{
public partial class CustomResources : ResourceDictionary
{
public void MouseEnter(object sender, EventArgs e)
{
MessageBox.Show("Test");
}
}
}
- 现在,在XAML中设置
x:Class
属性,并将处理程序设置为MouseEnter
XAML:
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication1.CustomResources"
xmlns:local="clr-namespace:WpfApplication1">
<ControlTemplate x:Key="TitledWindowControlTemplateKey"
x:Name="PART_ControlTemplate"
TargetType="{x:Type local:TitleWindow}">
<Rectangle>
<Rectangle.Style>
<Style TargetType="Rectangle">
<EventSetter Event="Mouse.MouseEnter" Handler="MouseEnter"/>
</Style>
</Rectangle.Style>
</Rectangle>
</ControlTemplate>
</ResourceDictionary>
您想做什么?我想通过XAML@Jovan-您不能使用资源字典的代码隐藏中的方法吗?我不认为您可以像这样将ObjectDataProvider挂接到RouteEvent,也不能挂接到委托具有不同签名的位置。那么,我如何才能挂接到此方法或任何方法呢?请检查我的答案,以了解ResourceDictionary中是否有代码。但是为什么模板
需要知道她属于哪个类,当我尝试绑定到对象并通过StaticResource键提取其方法时。。。顺便说一句,如果你想查看我的回答:我回答了你的问题等等。。。xD这很好,但是如果我在TitledWindow类中定义了这些方法,会怎么样?因为这是使用此ResourceDictionary的类?为此,您必须在该类上创建ICommand
,并在此处使用交互触发器绑定到该命令。但是,从ResourceDictionary直接绑定到方法是不可能的。或者您可以将此资源完全移动到窗口下。参考资料
部分,您可以直接从后面的代码钩住处理程序。啊,好吧,我想我必须使用交互性触发器,我读过关于它们的内容,但我希望有更优雅的内容,谢谢!首先,非常感谢您的提问和回答。我有完全相同的问题,但我想有复选框检查或取消检查回调。我已经在你的回答中添加了MessageBox,以查看是否调用了正确的回调。我目前的问题是,我的回调函数CheckBox\u Checked甚至在窗口显示之前就被调用了,我得到了一个异常。PresentationFramework.dll中发生“System.InvalidOperationException”类型的未处理异常。恐怕应用程序尚未完全初始化。知道吗,我如何检查init是否完成了?
using System;
using System.Windows;
namespace WpfApplication1
{
public partial class CustomResources : ResourceDictionary
{
public void MouseEnter(object sender, EventArgs e)
{
MessageBox.Show("Test");
}
}
}
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApplication1.CustomResources"
xmlns:local="clr-namespace:WpfApplication1">
<ControlTemplate x:Key="TitledWindowControlTemplateKey"
x:Name="PART_ControlTemplate"
TargetType="{x:Type local:TitleWindow}">
<Rectangle>
<Rectangle.Style>
<Style TargetType="Rectangle">
<EventSetter Event="Mouse.MouseEnter" Handler="MouseEnter"/>
</Style>
</Rectangle.Style>
</Rectangle>
</ControlTemplate>
</ResourceDictionary>