Wpf 在XAML中设置UserControl属性

Wpf 在XAML中设置UserControl属性,wpf,xaml,Wpf,Xaml,我想在XAML中设置代码隐藏中的自定义属性 财产: Public Property WindowName() As String Implements IVendorEntryEditView.WindowName Get Return CType(GetValue(WindowNameProperty), Integer) End Get Set(ByVal value As String) SetValue(WindowNameProp

我想在XAML中设置代码隐藏中的自定义属性

财产:

Public Property WindowName() As String Implements IVendorEntryEditView.WindowName
    Get
        Return CType(GetValue(WindowNameProperty), Integer)
    End Get
    Set(ByVal value As String)
        SetValue(WindowNameProperty, value)
    End Set
End Property

Public Shared ReadOnly WindowNameProperty As DependencyProperty = _
    DependencyProperty.Register("WindowName", GetType(String), GetType(VendorEntryEditView), _
        New PropertyMetadata(""))
但是,当我尝试设置时,我在XAML中得到一个
附加属性没有setter
消息:

<UserControl x:Class="VendorEntryEditView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:local="clr-namespace:EntryEditUi"
         mc:Ignorable="d"
         Loaded="VendorEntryEditView_OnLoaded"
         local:VendorEntryEditView.WindowName="test"
         d:DesignHeight="300" d:DesignWidth="300">

如何在XAML中设置该属性?谢谢

您可以尝试一下(在您使用
UserControl
时):


为什么需要在XAML中设置它?新的PropertyMetadata(“”)允许您为属性指定默认值。这就是您想要实现的吗?我想在XAML中设置它,因为它是从我们的视图基类继承的属性(稍后我从代码示例中删除了该属性),将被重用,并且需要轻松设置。我认为XAML是最容易的地方。请尝试从XAML中的属性中删除
本地:VendorEntryEdit视图。
前缀。@RichardDeeming-不起作用。@MikeCole:您可能还需要将开始和结束
标记更改为
,其中
YourViewBase
是您的视图基类,然后将
local
映射到正确的名称空间。第二种方法虽然不是很简单,但有效。是的,简单的方法通常是在注册DP时在代码中指定DefaultValue。如果任何未来的开发人员查看您的代码,发现这是一个自定义DP,他们应该知道在哪里检查默认值。另一个选项是直接继承,基类声明此DP。
<local:VendorEntryEditView WindowName="Test Value" />
<UserControl.Style>
  <Style>
    <Setter Property="local:VendorEntryEditView.WindowName"
            Value="Blah Name" />
  </Style>
</UserControl.Style>