【问题标题】:Cannot set multibinding无法设置多重绑定
【发布时间】:2017-07-20 16:06:00
【问题描述】:

我的多重绑定不工作。我收到错误:名称 '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();
            }
        }
    }
}

【问题讨论】:

  • 为什么你的ResultPopUp 类实现了IMultiValueConverter?您是否尝试过将您的 MatrixToDataViewConverter 放在单独的文件中,或者至少将 ResultPopUp 放在外部以便可以访问?
  • 为什么将MatrixToDataViewConverter定义为嵌套类?
  • 抱歉,我忘记将 IMultiValueConverter 从 ResultPopUp 类中取出。我试图把它放在一个单独的类中,但它不起作用,我得到了同样的错误。 @MightyBadaboom
  • 查看我的回答以获取更多信息。

标签: c# wpf xaml


【解决方案1】:

问题是,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 设计器并重新打开它。

【讨论】:

  • 哦,我觉得自己很愚蠢。太感谢了!现在完美运行。
【解决方案2】:

您不能将IValueConverterIMultiValueConverter 定义为嵌套类。只需将它放在一个单独的文件中,或者至少放在您的 ResultPopUp 类之外。

欲了解更多信息,请查看:Binding converter as inner class?

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

【讨论】:

    猜你喜欢
    • 2013-10-30
    • 2018-10-26
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-02
    相关资源
    最近更新 更多