我想了幾個自定義屬性在我的應用程序添加到文本框,它會幫助我減少大幅所需的代碼行..如何添加附加屬性的文本框
C#代碼添加屬性
class HorizonTextBoxExt:TextBox
{
public HorizonTextBoxExt() : base() { }
public bool BoundToDataGrid
{
get { return (bool)this.GetValue(BoundToDataGridProperty); }
set { this.SetValue(BoundToDataGridProperty, value); }
}
public static readonly DependencyProperty BoundToDataGridProperty =
DependencyProperty.Register(
"BoundToDataGrid",
typeof(bool),
typeof(HorizonTextBoxExt),
new UIPropertyMetadata(false)
);
}
「錯誤3所述的附接的屬性 'BoundToDataGrid' 未在 型HorizonTextBoxExt」找到。
是我在WPF設計
<Window x:Class="WpfApplication7.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Local ="clr-namespace:WpfApplication7"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox HorizontalAlignment="Left" Height="63" Margin="90,47,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="225" Local:HorizonTextBoxExt.BoundToDataGrid="true" />
</Grid>
</Window>
你也許打算創建[附加屬性] (http://msdn.microsoft.com/en-us/library/ms749011%28v=vs.110%29.aspx)?至少XAML中的屬性用法讓我覺得如此。 – Clemens