Cannot set multibinding because multivalueconverter must be specified WPF

帅比萌擦擦* 提交于 2019-12-11 18:26:27

问题


i have trouble with WPF converter, it give me this error "Cannot set multibinding because multivalueconverter must be specified WPF". I look in some forums and found some informations , but its still show error

My .cs code:

namespace Scroll4
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                //WindowState = WindowState.Maximized;
                InitializeComponent();
            }
            public class ScrollOffsetToVisibilityConverter : IMultiValueConverter
            {
                public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
                {
                    if (values == null)
                        throw new ArgumentException("Values cannot be null.");
                    if (values.Count() != 2)
                        throw new ArgumentException("Incorrect number of bindings (" + values.Count() + ")");
                    if (parameter == null)
                        throw new ArgumentException("Parameter cannot be null.");

                    var top = parameter.ToString().ToUpper() == "TOP";

                    var offset = Double.Parse(values[0].ToString());
                    var maxHeight = Double.Parse(values[1].ToString());

                    return (top && offset == 0) || (!top && offset == maxHeight) ? Visibility.Visible : Visibility.Collapsed;
                }

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

My xaml:

<Window x:Class="Scroll4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:Scroll4"
        Title="MainWindow" Height="350" Width="525">

        <Window.Resources>
                 <!--ERORR-->   
                 <local:ScrollOffsetToVisibilityConverter x:Key="Converter" />
                 <SolidColorBrush x:Key="Background" Color="Gray" />...

Maybe you know how to fix it?


回答1:


Seems to be incorrect reference only. And there are changes in the converter. Array is having Count() method. Might be you used system.linq. Just change the wpfApplication1 with your namespace

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            //WindowState = WindowState.Maximized;
            InitializeComponent();
        }
    }
    public class ScrollOffsetToVisibilityConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values == null)
                throw new ArgumentException("Values cannot be null.");
            if (values.Length != 2)
                throw new ArgumentException("Incorrect number of bindings (" + values.Length + ")");
            if (parameter == null)
                throw new ArgumentException("Parameter cannot be null.");

            var top = parameter.ToString().ToUpper() == "TOP";

            var offset = Double.Parse(values[0].ToString());
            var maxHeight = Double.Parse(values[1].ToString());

            return (top && offset == 0) || (!top && offset == maxHeight) ? Visibility.Visible : Visibility.Collapsed;
        }

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

XAML File:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:WpfApplication1" >
    <Window.Resources>
        <local:ScrollOffsetToVisibilityConverter x:Name="ConverterName"  x:Key="Converter"/>
    </Window.Resources>
    <Grid>
        <TextBox Text="Hi"/>
    </Grid>
</Window>


来源:https://stackoverflow.com/questions/31424364/cannot-set-multibinding-because-multivalueconverter-must-be-specified-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!