在XAML中设置UserControl属性

时间:2013-04-24 15:54:43

标签: wpf xaml

我在Code Behind中有自定义属性,我想在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中收到Attached property has no 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中设置该属性?谢谢!

1 个答案:

答案 0 :(得分:1)

也许只是尝试(使用UserControl的地方):

<local:VendorEntryEditView WindowName="Test Value" />

如果您必须在UserControl本身的xaml中指定此值,请通过Style

中的VendorEntryEditView.xaml进行分配
<UserControl.Style>
  <Style>
    <Setter Property="local:VendorEntryEditView.WindowName"
            Value="Blah Name" />
  </Style>
</UserControl.Style>