无法设置多重绑定

时间:2017-07-12 11:56:30

标签: c# wpf xaml

我的多重绑定不起作用。我收到错误:名称' MatrixToDataViewConverter'在我的xaml中没有存在于命名空间' clr-NameSpace:myNamespace' 中(我已标记了该行)。为什么?

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文件:

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();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:4)

问题是,MatrixToDataViewConverter是一个嵌套类。像这样重构cs文件:

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();
        }
    }
}

然后在您的解决方案上执行CleanRebuild。关闭XAML设计器并重新打开它。

答案 1 :(得分:2)

您无法将nilIValueConverter定义为嵌套类。只需将其放在单独的文件中,或至少放在IMultiValueConverter课程之外。

有关详细信息,请查看:Binding converter as inner class?

也许你必须在重构后清理并重建你的解决方案。