0

我正在实现一个包含 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#

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.

关于如何添加此参考的任何建议?

4

1 回答 1

2

你只需要确保命名空间是对齐的。我首先将您的转换器类与externalClass该类分开(我不确定是否可以从 XAML 中引用类似的嵌套类):

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

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

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

这样,转换器应该可以按书面形式访问。

<local:PointCollectionConverter x:Key="pointCollectionConverter"/>
于 2012-11-30T09:11:29.797 回答