Skip to content

Guard BrushToHexConverter against non-brush input - #4123

Open
karlmoriaty wants to merge 1 commit into
MaterialDesignInXAML:masterfrom
karlmoriaty:fix/brushtohex-converter-type-check
Open

karlmoriaty wants to merge 1 commit into
MaterialDesignInXAML:masterfrom
karlmoriaty:fix/brushtohex-converter-type-check

Conversation

@karlmoriaty

@karlmoriaty karlmoriaty commented Nov 12, 2026 •

Copy link
Copy Markdown

tl;dr

BrushToHexConverter casts its input straight to SolidColorBrush. In the XAML
designer that input can be the mock view model, so the cast throws and the design
surface fails with the error from #1411. I added a type check.

What

One line in src/MaterialDesignDemo.Shared/Converters/BrushToHexConverter.cs:
the explicit cast is replaced by a pattern match that returns null when the
input is not a brush.

Why

ColorTool.xaml binds the hex labels like this:

<TextBlock DataContext="{DynamicResource PrimaryHueMidBrush}"
           Text="{Binding Converter={StaticResource BrushToHexConverter}}" />

At runtime PrimaryHueMidBrush always resolves, so the converter always gets a
brush and nothing changes.

In the designer the lookup can fail. DataContext then falls back to the
inherited one, which is the mock object the designer creates for
d:DesignInstance Type=sharedDomain:ColorToolViewModel. The cast throws:

Unable to cast object of type 'Mocks.MaterialDesignDemo_ColorToolViewModel_...'
to type 'System.Windows.Media.SolidColorBrush'

That is #1411. With the type check the label renders empty in the designer
instead of breaking the design surface.

Scope

Type guard only. The design-time view model suggested in #1411 would also give
the designer real data, but that is a bigger change and I would rather keep
this one small.

Fixes #1411

Verification

  • dotnet build src/MainDemo.Wpf/MaterialDesignDemo.csproj -f net8.0-windows: 0 warnings, 0 errors
  • Runtime behaviour unchanged: every binding that uses this converter passes a SolidColorBrush

The converter casts its input straight to SolidColorBrush. At design time the
DataContext of the hex labels can fall back to the mock view model the XAML
designer builds for d:DesignInstance, so that cast throws and the designer
reports the error described in MaterialDesignInXAML#1411. Return null for anything that is not a
SolidColorBrush; the runtime path always passes a brush, so its behaviour is
unchanged.

Assisted-by: DeepSeek:deepseek-v4.1-flash:WorkBuddy
Signed-off-by: karlmoriaty <3070687583@qq.com>
@karlmoriaty

Copy link
Copy Markdown
Author

@Keboo the Pull Request Verification workflow is waiting on first-time contributor approval. Could you approve it when you get a chance? The change is the one-line cast replacement described above.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to cast object of type 'Mocks.MaterialDesignDemo_ColorToolViewModel_2_47133230' to type 'System.Windows.Media.SolidColorBrush'.

1 participant