【发布时间】:2021-07-16 09:47:24
【问题描述】:
我有这个模型
[NotifyPropertyChanged]
public class WidgetConfiguration
{
#region Properties
#endregion Properties
}
我在 ViewModel 中将其用于 Collection 和 Selected 项目属性(ListView / GridView SelectedItem="{Binding SelectedWidget}" ...)
[NotifyPropertyChanged]
public class WidgetViewModel
{
public ObservableCollection<WidgetConfiguration> Configurations { get; set; } = new ObservableCollection<WidgetConfiguration>();
public WidgetConfiguration SelectedWidget { get; set; }
}
然后我想将 SelectedWidget 绑定到作为 SelectedItem 编辑器的 UserControl:
<controls:WidgetConfig Widget="{Binding SelectedWidget}" />
UserControl 是这样定义的(使用 PostSharp 声明 DependencyProperties)
[NotifyPropertyChanged]
public partial class WidgetConfig : UserControl
{
[DependencyProperty]
public WidgetConfiguration Widget { get; set; }
public WidgetConfig()
{
InitializeComponent();
this.DataContext = this;
}
}
但我在 UserControl 绑定上遇到错误:
严重性代码描述项目文件行抑制状态错误A 无法在类型的“小部件”属性上设置“绑定” 'Squiddy_Client_Views_WidgetConfig_10_577403948'。 “绑定”只能 在 DependencyProperty 上设置 依赖对象。客户端 C:\develop\Squiddy\Client\Views\WidgetManager.xaml 21
我已经尝试手动实现 DependencyProperties 并确保所有类型都是正确的,甚至是默认类型和默认值。它没有帮助。
我已经阅读了谷歌上的所有结果,但不知道该怎么办。
这是可能的还是我需要进行代理绑定?
编辑:
只是为了它,我尝试手动实现 DependencyProperty:
public static readonly DependencyProperty WidgetProperty =
DependencyProperty.Register("Widget", typeof(WidgetConfiguration),
typeof(WidgetConfig));
[SafeForDependencyAnalysis]
public WidgetConfiguration Widget
{
get { return GetValue(WidgetProperty) as WidgetConfiguration; }
set { SetValue(WidgetProperty, value); }
}
现在 XAML 错误已消失,但绑定已“失效”。在 ListView 中选择新对象时,UserControl 不会更新:
- PropertySetter 没有被调用
- ViewModel 上的 PropertyChanged 事件确实会发生...
编辑 2:
我完全错过了 PostSharp 文档中的这一部分,我没有添加 DependencyProperty 以及属性。 (感谢丹尼尔·巴拉斯)
public static DependencyProperty WidgetProperty { get; private set; }
[DependencyProperty]
public WidgetConfiguration Widget { get; set; }
编辑 3:
看完这个视频我终于找到了DataContext/root的答案: https://www.youtube.com/watch?v=h7ZrdGiOm3E
- 我从 UserControl 构造函数中删除了“this.DataContext = this”
- 我在 XAML 的 UserControl 元素中添加了一个 Name="root"
- UserControl 内的绑定应指向 ElementName=root 并使用属性 Widget.xxx
像这样:
<UserControl Name="root">
<TextBlock Text="{Binding Header, ElementName=root}"></TextBlock>
<Label Content="{Binding Widget.Name, ElementName=root}" />
</UserControl>
【问题讨论】:
标签: mvvm dependency-properties inotifypropertychanged postsharp