访问用户类方法

时间:2014-08-20 11:28:04

标签: wpf styles

我做了以下问题,已经回答了,

WPF: Trying to add a class to Window.Resources Again

但还有另一个问题:当我尝试访问" CenterToolTipConverter"通过样式资源的方法,消息出现:"资源无法解析"

CenterToolTipConverter.cs

namespace WpfApplication1
{
    public class CenterToolTipConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values.FirstOrDefault(v => v == DependencyProperty.UnsetValue) != null)
            {
                return double.NaN;
            }
            double placementTargetWidth = (double)values[0];
            double toolTipWidth = (double)values[1];
            return (placementTargetWidth / 2.0) - (toolTipWidth / 2.0);
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication1;assembly=WpfApplication1"
    Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <local:CenterToolTipConverter x:Key="myCenterToolTipConverter"/>
    </Window.Resources>
</Window>
直到这里,没问题。但是,当我尝试访问&#34; CenterToolTipConverter&#34;方法,在:

Styles.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:sys="clr-namespace:System;assembly=mscorlib">

    <Style TargetType="{x:Type ToolTip}">
        <Setter Property="HorizontalOffset">
            <Setter.Value>
                <MultiBinding Converter="{StaticResource myCenterToolTipConverter}">
                    <Binding RelativeSource="{RelativeSource Self}" Path="PlacementTarget.ActualWidth"/>
                    <Binding RelativeSource="{RelativeSource Self}" Path="ActualWidth"/>
                </MultiBinding>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

该消息错误发生(&#34;资源无法解析&#34;)。这有什么不对?在此先感谢!!

1 个答案:

答案 0 :(得分:1)

您已将转换器添加为Window中的资源,但ResourceDictionary不了解Window中定义的资源。

ResourceDictionary中定义转换器,或在应用程序级别将两个资源添加到App.xaml.

相关问题