lepoco / lepoco/wpfui

InvalidCastException in Dialog.get_DialogWidth()

Open
#415 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C#
Stars
9.6k
Forks
1k
PR merge metrics
No merged PRs in 30d

Description

Describe the bug

When retrieving the DialogWidth or DialogHeight properties in an existing Dialog control, an InvalidCastException is thrown.

To Reproduce
  1. Create a Dialog control in XAML with assigned DialogWidth/DialogHeight properties.
<!-- ... -->
    xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
<!-- ... -->
<ui:Dialog x:Name="SomeDialog" Title="Cool Title" ButtonLeftAppearance="Danger" ButtonLeftName="Bad Button" ButtonRightAppearance="Secondary" ButtonRightName="Meh Button" DialogHeight="300" DialogWidth="250"/>
  1. Attempt to retrieve the fields programmatically.
using System.Diagnostics;
// ...
void SomeMethod() {
    Debug.WriteLine($"Dimensions are: {SomeDialog.DialogWidth:N2}x{SomeDialog.DialogHeight:N2}");
}
  1. An exception of type InvalidCastException is thrown (see screenshots).
Expected behavior

No exception is raised, and the value is retrieved.

Screenshots

image

image

OS version

Windows 10 (Version 21H2, OS Build 19044.2006)

.NET version

.NET 6.0

WPF-UI NuGet version

2.0.2

Additional context

Looking into the source code, this appears to be caused by an (allegedly) non-essential type cast.

In Dialog.cs, the DialogWidth and DialogHeight dependency properties are of type System.Double.

    /// <summary>
    /// Property for <see cref="DialogWidth"/>.
    /// </summary>
    public static readonly DependencyProperty DialogWidthProperty =
        DependencyProperty.Register(nameof(DialogWidth),
            typeof(double), typeof(Dialog), new PropertyMetadata(420.0));

    /// <summary>
    /// Property for <see cref="DialogHeight"/>.
    /// </summary>
    public static readonly DependencyProperty DialogHeightProperty =
        DependencyProperty.Register(nameof(DialogHeight),
            typeof(double), typeof(Dialog), new PropertyMetadata(200.0));

However, the property getters cast this value to System.Int32 before returning the result.

    /// <inheritdoc />
    public double DialogWidth
    {
        get => (int)GetValue(DialogWidthProperty);
        set => SetValue(DialogWidthProperty, value);
    }

    /// <inheritdoc />
    public double DialogHeight
    {
        get => (int)GetValue(DialogHeightProperty);
        set => SetValue(DialogHeightProperty, value);
    }

This last second cast to int may be what causes the InvalidCastException (as seen in the screenshot)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/Wpf.Ui/Controls/Dialog.cs and inspect the DialogWidth and DialogHeight dependency properties and their getters. Reproduce the XAML example on .NET 6/WPF-UI 2.0.2, retrieve both properties, and confirm the values are returned without InvalidCastException.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
desktop, frontend
Issue type
Bug
Difficulty
1/5
Estimated time
Under an hour
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.