2013-04-24 79 views
0

我在我想在XAML中設置的代碼後面有自定義屬性。在XAML中設置UserControl屬性

物業:

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?謝謝!

+0

爲什麼你需要在XAML中設置它?新的PropertyMetadata(「」)允許您指定該屬性的默認值。 這就是你想要達到的目標嗎? – Miiite 2013-04-24 16:17:41

+0

我想在XAML中設置它,因爲它是我們View Base類的繼承屬性(我稍後從代碼示例中刪除)將被重用並需要輕鬆設置。我認爲XAML將是最簡單的地方。 – 2013-04-24 16:26:41

+0

嘗試從您的XAML中的屬性中刪除'local:VendorEntryEditView.'前綴。 – 2013-04-24 17:49:20

回答

1

也許只是嘗試(在您使用您的UserControl):

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

,如果你必須分配在UserControl本身的XAML這個值,通過它分配一個StyleVendorEntryEditView.xaml

<UserControl.Style> 
    <Style> 
    <Setter Property="local:VendorEntryEditView.WindowName" 
      Value="Blah Name" /> 
    </Style> 
</UserControl.Style> 
+0

第二種方法雖然不是非常簡單,但卻奏效。 – 2013-04-24 18:01:46

+0

Yeh直接的方法通常是在註冊DP時在代碼隱藏中分配DefaultValue。如果任何未來的開發人員查看您的代碼並查看定製的DP,他們應該查看在哪裏查看Defaults。你的另一個選擇就是直接繼承 - 基類聲明這個DP。 – Viv 2013-04-24 18:07:40

相關問題