coder
coder

Reputation: 558

Cannot set multibinding

My multibinding is not working. I get the error: The name 'MatrixToDataViewConverter' does not exist in the namespace 'clr-NameSpace: myNamespace' in my xaml (I have marked the line). Why?

xaml

 <Window x:Class="myNamespace.PopMeUp"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:app="clr-namespace:myNamespace"
            Title="PopMeUp" Height="300" Width="300">
        <Window.Resources>
            <app:MatrixToDataViewConverter x:Key="MatrixToDataViewConverter"/> <!-- Error here-->
        </Window.Resources>
        <Grid>
            <DataGrid>
                <DataGrid.ItemsSource>
                    <MultiBinding Converter="{StaticResource MatrixToDataViewConverter}">
                        <Binding Path="ColumnHeaders"/>
                        <Binding Path="RowHeaders"/>
                        <Binding Path="Values"/>
                    </MultiBinding>
            </DataGrid.ItemsSource>
          </DataGrid>
        </Grid>
    </Window>

.cs file:

namespace myNamespace    
{
    /// <summary>
    /// Interaction logic for PopMeUp.xaml
    /// </summary>
    public partial class PopMeUp : Window
    {
        public PopMeUp(MWArray[] Result, int rows, int columns)
        {
            InitializeComponent();
        }

        public class MatrixToDataViewConverter : IMultiValueConverter
        {
            public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
            {
                var myDataTable = new DataTable(); 
                return myDataTable.DefaultView;
            }


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

Upvotes: 3

Views: 836

Answers (2)

Mighty Badaboom
Mighty Badaboom

Reputation: 6155

You can not define an IValueConverter or IMultiValueConverter as a nested class. Just put it in a seperate file or at least outside your ResultPopUp class.

For further information have a look at: Binding converter as inner class?

Maybe you have to cleanup and rebuild your solution after the refactoring.

Upvotes: 2

Romano Zumb&#233;
Romano Zumb&#233;

Reputation: 8099

The problem is, that MatrixToDataViewConverter is a nested class. Refactor the cs file like this:

namespace myNamespace    
{
    /// <summary>
    /// Interaction logic for ResultPopUp.xaml
    /// </summary>
    public partial class ResultPopUp : Window
    {
        public ResultPopUp(MWArray[] Result, int rows, int columns)
        {
            InitializeComponent();
        }        
    }

    public class MatrixToDataViewConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            var myDataTable = new DataTable(); 
            return myDataTable.DefaultView;
        }

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

Then perform a Clean and Rebuild on your solution. Close the XAML designer and reopen it.

Upvotes: 4

Related Questions