簡體   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