添加成员方法作为资源

时间:2012-11-30 09:07:57

标签: wpf resources

我正在实现一个包含ObservableCollection的类,在我的XAML中我有一条折线。 我成功地将XAML dataContext绑定到此类,并将折线绑定到Observable集合。但是现在,当然,我面临着错误的类型转换。 我找到了一个值转换器的例子,我把它添加到我的代码中,但是我无法将它作为资源添加到我的XAML中......

整体结构看起来像

public class externalClass
{
    public ObservableCollection<Point> debugCh1 { get; set; }

    public void test() 
    { 
         ... performo modifications
          on debugCh1 for testing purposes...
    }

    public class PointCollectionConverter : IValueConverter
    {
        .. implements convert and cnverBack
    }
}

对于XAML

<Window x:Class="tester.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="277" Width="525" xmlns:my="clr-namespace:binding;assembly=binding" xmlns:my1="clr-namespace:deviceManager;assembly=deviceManager" Closing="Window_Closing">


    <Window.Resources>
        <local:PointCollectionConverter x:Key="pointCollectionConverter"/>
    </Window.Resources>


           ... The window Itself ...
        <Polyline Points="{Binding debugCh1}" />
           ...
</Window>

对于C#背后的<#p>

public partial class MainWindow : Window
{ 
    private externalClass toTest;

    public MainWindow()
    {
        InitializeComponent();
        DataContext = toTest;
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        toTest.test();
    }
}

对于<local:PointCollectionConverter..

,整个过程很有效

编译器说The type 'local:PointCollectionConverter' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built.

有关如何添加此引用的任何建议?

1 个答案:

答案 0 :(得分:2)

您只需要确保命名空间排成一行。我首先将转换器类与externalClass类分开(我不确定甚至可以从XAML引用类似嵌套类的类):

namespace MyCompany.MyProject
{
    public class PointCollectionConverter : IValueConverter
    {
        .. implements convert and cnverBack
    }
}

现在您可以定义local xmlns并将其链接到MyCompany.MyProject

<Window xmlns:local="clr-namespace:MyCompany.MyProject"

由此可以按照书面形式访问转换器。

<local:PointCollectionConverter x:Key="pointCollectionConverter"/>