2 Replies - 1378 Views - Last Post: 22 November 2016 - 01:46 PM Rate Topic: -----

#1 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 310
  • Joined: 13-March 12

MultiBinding Converter Error

Posted 22 November 2016 - 11:57 AM

I am trying out a multi binding converter. But it is throwing a casting exception

"System.InvalidCastException was unhandled
_HResult=-2147467262
_message=Unable to cast object of type 'MS.Internal.NamedObject' to type 'System.IConvertible'.
HResult=-2147467262
IsTransient=false
Message=Unable to cast object of type 'MS.Internal.NamedObject' to type 'System.IConvertible'.
Source=mscorlib
StackTrace:
at System.Convert.ToByte(Object value)
at ColorPicker.RGBConverter.System.Windows.Data.IMultiValueConverter.Convert(Object[] values, Type targetType, Object parameter, CultureInfo culture) in c:\Users...RGBConverter.cs:line 17
"

There's more but that points to this line;

 _brush.Color = Color.FromRgb(System.Convert.ToByte(values[0]), System.Convert.ToByte(values[1]), System.Convert.ToByte(values[2]));


The MainWindow
<Window x:Class="ColorPicker.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vw="clr-namespace:ColorPicker.View"
        Title="MainWindow" Height="350" Width="525">
    <GroupBox
        Header="Color Picker"
        BorderBrush="Gray"
        BorderThickness="2"
        >
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition  Width="Auto"/>
                <ColumnDefinition Width=".25*"/>
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
        
            <DockPanel 
                Grid.Row="0"
                Grid.RowSpan="4"
                Grid.Column="0"
                >
                
                <Slider Minimum="0"
                        Maximum="255"
                        Value="0"
                        Orientation="Vertical"
                        Width="25"
                        VerticalAlignment="Stretch"
                        Background="Red"
                        DockPanel.Dock="Left"
                        Margin="4"
                        SnapsToDevicePixels="True"
                        x:Name="_red"
                        >
                </Slider>
                <Slider Minimum="0"
                        Maximum="255"
                        Value="0"
                        Orientation="Vertical"
                        Width="25"
                        VerticalAlignment="Stretch"
                        Background="Green"
                        DockPanel.Dock="Left"
                        Margin="4"
                        SnapsToDevicePixels="True"
                        x:Name="_green"
                        >
                </Slider>
                <Slider Minimum="0"
                        Maximum="255"
                        Value="0"
                        Orientation="Vertical"
                        Width="25"
                        VerticalAlignment="Stretch"
                        Background="Blue"
                        DockPanel.Dock="Left"
                        Margin="4"
                        SnapsToDevicePixels="True"
                        x:Name="_blue"
                        >
                </Slider>
            </DockPanel>
            <vw:Swatch 
                Grid.Row="0"
                Grid.Column="2"
                Grid.RowSpan="2"
                Grid.ColumnSpan="2"
                >
            </vw:Swatch>
        </Grid>
    </GroupBox>
</Window>



And the UserControl;

<UserControl x:Class="ColorPicker.View.Swatch"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:cv="clr-namespace:ColorPicker"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    
    <UserControl.Resources>
        <cv:RGBConverter x:Key="rgbConverter"/>
    </UserControl.Resources>
    
    <Rectangle
        Stroke="black"
        StrokeThickness="2"
        >
        <Rectangle.Fill>
            <MultiBinding Converter="{StaticResource rgbConverter}">
                <Binding ElementName="_red" Path="value" />
                <Binding ElementName="_green" Path="value"/>
                <Binding ElementName="_blue" Path="value"/>
            </MultiBinding>
        </Rectangle.Fill>
            
    </Rectangle>
</UserControl>



And the evil perpetrator of exceptions

namespace ColorPicker
{
    public class RGBConverter : IMultiValueConverter
    {
        SolidColorBrush _brush = new SolidColorBrush();

        object IMultiValueConverter.Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            _brush.Color = Color.FromRgb(Convert.ToByte(values[0]), Convert.ToByte(values[1]), Convert.ToByte(values[2]));
            return _brush;
        }

        object[] IMultiValueConverter.ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}



I just don't know what the problem is. I can't see what I am tring to cast that this line doesn't like. I simply want to combine the three values into a final brush for the foreground of the rectangle.

Thanks for the help.

This post has been edited by ScottinTexas: 22 November 2016 - 11:59 AM


Is This A Good Question/Topic? 0
  • +

Replies To: MultiBinding Converter Error

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: MultiBinding Converter Error

Posted 22 November 2016 - 01:05 PM

values[x] is still an object. That's how it arrived, as an array of objects.
I don't think you can cast from object to byte.
Try first casting the element to an int, then that to a byte. If nothing else you'll have something to debug.

int red = (int)values[0];
int green = (int)values[1];
//etc.

Was This Post Helpful? 0
  • +
  • -

#3 ScottinTexas   User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 310
  • Joined: 13-March 12

Re: MultiBinding Converter Error

Posted 22 November 2016 - 01:46 PM

Thanks. It appears that there is no value passed when the MultiConverter is called

Attached Image

So it can't convert a nothing. I'm going to change things around a little and bind the individual sliders to properties, then bind the rectangle to a MultiConverter that gets the values from the properties. Then I know for certain that there are values to be converted. However, I do have the initial values of the sliders set to zero.

I thank you, once again, for your reply. I'll see what kind of a mess I can make of this. I'm only doing this for fun. It's not being very fun right now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1