繁体   English   中英

绑定/引用XAML WPF的方法

[英]Binding/Referencing Method to XAML WPF

我有这个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"
                    >
    <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>

我的c#代码:

namespace My.Windows
{
    public partial class TitledWindow : Window
    {
        public void Test()
        {
            MessageBox.Show("Test");
        }
    }
}

问题是我收到以下错误:

错误1
'ResourceDictionary'根元素需要ax:Class属性来支持XAML文件中的事件处理程序。 删除MouseEnter事件的事件处理程序,或将ax:Class属性添加到根元素。

那么你可以通过将代码附加到ResourceDictionary来做到这一点。 实现这一目标的几个简单步骤是:

  • 假设ResourceDictionary文件名是CustomResources.xaml 在ResourceDictionary之外的同一目录中添加另一个文件,名称为CustomResources.xaml.cs 创建继承自ResourceDictionary的partial class 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>

问题是Template需要知道它应用于什么具有MouseEnter 不幸的是,即使将x:Type应用于模板,xaml编译器也没有足够的功能继续下去。

我之前做过类似的事情,让ResourceDictionary识别出我想要的东西的孔隙,看起来我用一种风格来绕过它。 http://winchrome.codeplex.com/SourceControl/latest#WinChrome/UI/VS2012ResourceDictionary.xaml中的完整代码。

<ResourceDictionary ... >

<Style x:Key="CloseButtonStyle" TargetType="{x:Type Button}" >
  ...
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Border x:Name="bd" ....>
                ....
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True" SourceName="bd">
                        <Setter Property="Background" TargetName="bd" Value="{DynamicResource {x:Static SystemColors.ControlLightLightBrushKey}}"/>
                        ... 
                    </Trigger>
                    <Trigger Property="IsPressed" Value="True">
                        <Setter Property="Background" TargetName="bd">
                          ...
                        </Setter>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

但是,您希望通过{StaticResource ...}将处理程序绑定到objectDataPresenter上的方法,我不确定您是否可以。 相反,你最好使用普通绑定绑定到DataContext {Binding Path=...} ,我想你仍然可以通过{StaticResource.. }提供DataContext

您需要添加x:class属性并指定资源的位置以及事件处理程序的位置。 请参阅是否可以在WPF中为资源字典设置代码以进行事件处理? 举个例子。

暂无
暂无

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

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