javirs
javirs

Reputation: 1100

Add member method as a resource

I'm implementing a class that contains an ObservableCollection and in my XAML I have a polyline. I successfully binded the XAML dataContext to the this class and the polyline to the Observable collection. But now, of course, I'm facing a wrong type conversion. I found an example of value converter and I added it to my code but I'm not able to add it as a resource in my XAML ...

The overall structure looks like that

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
    }
}

For the 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>

And for the C# behind

public partial class MainWindow : Window
{ 
    private externalClass toTest;

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

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

The whole thing works nice but for the <local:PointCollectionConverter..

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

Any suggestions on how to add this reference??

Upvotes: 0

Views: 605

Answers (1)

McGarnagle
McGarnagle

Reputation: 102783

You just have to make sure the namespaces are lined up. I'd start by separating your converter class from the externalClass class (I'm not sure it's even possible to reference nested-classes like that from XAML):

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

Now you can define the local xmlns and link it to MyCompany.MyProject:

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

And with that the converter should be accessible as written.

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

Upvotes: 2

Related Questions