CsWinRT icon indicating copy to clipboard operation
CsWinRT copied to clipboard

Bug: WMC1510 appearing but x:Bind is used

Open Sella-GH opened this issue 9 months ago • 6 comments

Description

Despite using {x:Bind ViewModel.x} the WMC1510 analyzer flags my XAML code. Apps works fine but the warning comes up when the app gets compiled.

Steps To Reproduce

  1. Create new App
  2. Use the following XAML Code
<ComboBox x:Name="ThemeMode"
          ItemsSource="{x:Bind ViewModel.Themes, Mode=OneWay}"
          DisplayMemberPath="DisplayName"
          SelectedValuePath="Value"
          SelectedValue="{x:Bind ViewModel.SelectedTheme, Mode=TwoWay}"
          PlaceholderText="Select Theme" />
  1. Use the following C# Code
public sealed partial class SettingsPageViewModel : ObservableObject
{
    [ObservableProperty]
    public partial ElementTheme SelectedTheme { get; set; } = ElementTheme.Default;

    public ObservableCollection<ThemeModel> Themes { get; } =
    [
        new(nameof(ElementTheme.Light), ElementTheme.Light),
        new(nameof(ElementTheme.Dark), ElementTheme.Dark),
        new(nameof(ElementTheme.Default), ElementTheme.Default)
    ];
}

[GeneratedBindableCustomProperty]
public sealed partial class ThemeModel(string displayName, ElementTheme value)
{
    public string DisplayName { get; init; } = displayName;
    public ElementTheme Value { get; init; } = value;
}
  1. Compile once
  2. See warning in output window (and then in error list if enabled)

Expected Behavior

The analyzer not flagging my code or some explanation why it does (and maybe how to correct my mistake).

Version Info

.NET SDK 9.0.201 Microsoft.CsWinRT 2.2.0 Microsoft.WindowsAppSDK 1.7.250208002-preview1

Additional Context

Interestingly this warning only occurs when the building of the application is done, not when it's just designed in the IDE.

Image

I also tried to specify x:DataType like it's said in the description but somehow I can't find a spot where it doesn't gets flagged as an error.

Sella-GH avatar Mar 15 '25 23:03 Sella-GH

The warning is showing because of your DisplayMemberPath="DisplayName". I think you need to use this attribute on your ViewModel class: [WinRT.GeneratedBindableCustomProperty([nameof(DisplayName)], [typeof(string)])]

tipa avatar May 22 '25 13:05 tipa

The warning is showing because of your DisplayMemberPath="DisplayName". I think you need to use this attribute on your ViewModel class: [WinRT.GeneratedBindableCustomProperty([nameof(DisplayName)], [typeof(string)])]

Sadly this doesn't help, the warning is still shown.

Sella-GH avatar May 22 '25 15:05 Sella-GH

I didn't test with ComboBox, but I assume you'd have to do the same thing for SelectedValuePath & Value, so something like

[WinRT.GeneratedBindableCustomProperty([nameof(DisplayName), nameof(Value)], [typeof(string), typeof(string)])]

tipa avatar May 22 '25 15:05 tipa

Sadly this doesn't help, the warning is still shown.

The warning doesn't disappear but you can ignore it if you configured it correctly AFAIK.

dongle-the-gadget avatar May 22 '25 16:05 dongle-the-gadget

The warning disappeared for me after I used the GeneratedBindableCustomProperty

tipa avatar May 22 '25 16:05 tipa

Sadly this doesn't help, the warning is still shown.

The warning doesn't disappear but you can ignore it if you configured it correctly AFAIK.

The example in the initial post still works so that's no issue at all. The warning is still there but I ignore it for now.

Sella-GH avatar May 22 '25 16:05 Sella-GH