InvalidCastException in Dialog.get_DialogWidth()
Nobody has claimed this yet.
- 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
- Create a Dialog control in XAML with assigned
DialogWidth/DialogHeightproperties.
<!-- ... -->
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"/>
- Attempt to retrieve the fields programmatically.
using System.Diagnostics;
// ...
void SomeMethod() {
Debug.WriteLine($"Dimensions are: {SomeDialog.DialogWidth:N2}x{SomeDialog.DialogHeight:N2}");
}
- An exception of type
InvalidCastExceptionis thrown (see screenshots).
Expected behavior
No exception is raised, and the value is retrieved.
Screenshots


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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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