如何向文本框添加其他属性

时间:2013-01-14 09:50:09

标签: c# wpf xaml textbox wpf-controls

我正在尝试在我的应用程序中向文本框中添加几个自定义属性,它将帮助我大幅减少所需的代码行。

用于添加属性的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'   输入Horizo​​nTextBoxExt'。

是我在wpf设计师的xaml风中得到的错误

    <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>

1 个答案:

答案 0 :(得分:3)

替换它:

<Grid>
    <TextBox /* ... */ Local:HorizonTextBoxExt.BoundToDataGrid="true" />

</Grid>

用这个:

<Grid>
    <Local:HorizonTextBoxExt /* ... */ BoundToDataGrid="true" />

</Grid>