Martyn Ball
Martyn Ball

Reputation: 4895

Aligning my Popup to bottom of Window

im trying to align my Popup to the bottom center of my Window, how ever i'm getting this error:

Additional information: Specified cast is not valid.

This is being caused by my converter double windowWidth = (double)values[0];, how ever the ActualWidth should bee a double! Not too sure on what is going wrong here.

I'm currently showing the data in a MessageBox just to test it at the moment and make sure the values look correct.

Converter

namespace Test_Project.Converters
{
    public class NotificationOffsets : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double windowWidth = (double)values[0];
            double notificationWidth = (double)values[1];

            MessageBox.Show("Notification Width: " + notificationWidth.ToString() + " Window Width: " + windowWidth.ToString());
            return false;
        }

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

XAML - Converter Binding

<Style TargetType="Popup" x:Key="PopupNotification">
    <Setter Property="IsOpen" Value="True" />
    <Setter Property="HorizontalOffset">
        <Setter.Value>
            <MultiBinding Converter="{StaticResource NotificationOffsets}">
                <Binding RelativeSource="{RelativeSource Self}" Path="PlacementTarget.ActualWidth" />
                <Binding RelativeSource="{RelativeSource Self}" Path="ActualWidth" />
            </MultiBinding>
        </Setter.Value>
    </Setter>
</Style>

Edit: Breakpoint Data: enter image description here

Edit 2: I have now set my PlacementTarget within my Style:

<Setter Property="PlacementTarget" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}" />

Still getting the same error!

Upvotes: 0

Views: 47

Answers (1)

Bruno Joaquim
Bruno Joaquim

Reputation: 1473

You have to return a double on your converter, you are returning a boolean, which cause the invalid cast.

EDIT: You are having Binding problems, you have to set the "PlacementTarget" of your popup in order the get the Width property.

EDIT 2: try this:

<Window x:Class="WpfApplication7.MainWindow"
        Name="myWindow"
    ............

<Setter Property="PlacementTarget" Value="{Binding ElementName=myWindow}"/>

Upvotes: 1

Related Questions