Visual Studio Intellisense在WPF中绑定时,不建议源生成的属性
我没有获得有关MVVM社区工具包生成的属性的Intellisense建议。有办法解决这个问题吗?如果我无法获得建议,那么使用[observableProperty]
甚至不值得IMO。下面的示例。
ViewModel具有一个生成的属性和一个手工制作属性:
public partial class SimplisticViewModel : ObservableObject
{
[ObservableProperty]
private int sourceGeneratedProperty;
private float handmadeProperty;
public float HandmadeProperty
{
get => handmadeProperty;
set => SetProperty(ref handmadeProperty, value);
}
}
XAML:
<Window x:Class="WPFEFTest.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFEFTest"
mc:Ignorable="d"
Title="TestWindow" Height="450" Width="800">
<Window.DataContext>
<local:SimplisticViewModel/>
</Window.DataContext>
<StackPanel>
<TextBox Text=""/>
</StackPanel>
</Window>
为手工制作属性设置绑定时,建议工作:
在设置源生成属性的绑定时,没有建议:
但是,如果我编写整个属性名称(避免错别字等),然后将光标悬停在它上(光标在屏幕截图中不可见),Intellisense将正确显示属性的类型。因此,问题可能不是Intellisense根本没有“看到”该属性。
编辑:要明确我不尝试绑定到一个字段,以下是MVVM社区的代码工具包由于observableProperty
属性而生成:
public partial class SimplisticViewModel
{
[global::System.CodeDom.Compiler.GeneratedCode("Microsoft.Toolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "7.1.0.0")]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public int SourceGeneratedProperty
{
get => sourceGeneratedProperty;
set
{
if (!global::System.Collections.Generic.EqualityComparer<int>.Default.Equals(sourceGeneratedProperty, value))
{
OnPropertyChanging(global::Microsoft.Toolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.SourceGeneratedPropertyPropertyChangingEventArgs);
sourceGeneratedProperty = value;
OnPropertyChanged(global::Microsoft.Toolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.SourceGeneratedPropertyPropertyChangedEventArgs);
}
}
}
}
I'm not getting intellisense suggestions for properties that were source generated by the MVVM community toolkit. Is there a way to fix this? If I can't get the suggestions to work, then using [ObservableProperty]
isn't even worth it IMO. Example below.
ViewModel has one source generated property and one handmade property:
public partial class SimplisticViewModel : ObservableObject
{
[ObservableProperty]
private int sourceGeneratedProperty;
private float handmadeProperty;
public float HandmadeProperty
{
get => handmadeProperty;
set => SetProperty(ref handmadeProperty, value);
}
}
XAML:
<Window x:Class="WPFEFTest.TestWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFEFTest"
mc:Ignorable="d"
Title="TestWindow" Height="450" Width="800">
<Window.DataContext>
<local:SimplisticViewModel/>
</Window.DataContext>
<StackPanel>
<TextBox Text=""/>
</StackPanel>
</Window>
When setting up binding for the handmade property, the suggestions work:
When setting up binding for the source generated property, there are no suggestions:
However, if I write the entire property name (avoiding typos and such...) and hover my cursor over it (cursor not visible in screenshot), intellisense will correctly show the type of the property. So the problem probably isn't that intellisense doesn't "see" the property at all.
EDIT: To make it clear I'm not trying to bind to a field, below is code that MVVM community toolkit generates because of the ObservableProperty
attribute:
public partial class SimplisticViewModel
{
[global::System.CodeDom.Compiler.GeneratedCode("Microsoft.Toolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator", "7.1.0.0")]
[global::System.Diagnostics.DebuggerNonUserCode]
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public int SourceGeneratedProperty
{
get => sourceGeneratedProperty;
set
{
if (!global::System.Collections.Generic.EqualityComparer<int>.Default.Equals(sourceGeneratedProperty, value))
{
OnPropertyChanging(global::Microsoft.Toolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.SourceGeneratedPropertyPropertyChangingEventArgs);
sourceGeneratedProperty = value;
OnPropertyChanged(global::Microsoft.Toolkit.Mvvm.ComponentModel.__Internals.__KnownINotifyPropertyChangedOrChangingArgs.SourceGeneratedPropertyPropertyChangedEventArgs);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您可以尝试设置
dataContext
for window ...当我删除d:dataContext
哪个指向viewModel类以及dataContext <时/代码>已设置,Intellisense从ViewModel拾取了属性。
Maybe you could try setting
DataContext
for window... Same thing/issue you describe happens when I removed:DataContext
which points to ViewModel class and whenDataContext
is set, Intellisense picks up properties from ViewModel.