Govind Mulleth
Govind Mulleth

Reputation: 1

How do I add additional properties to a text box

I'm trying to add a couple of custom properties to textboxes in my application , it will help me reduce the lines of code needed drastically..

C# Code for adding the property

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)
    );
}

"Error 3 The attachable property 'BoundToDataGrid' was not found in type HorizonTextBoxExt'.

is the error I get in the xaml wind of the wpf designer

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

Upvotes: 0

Views: 484

Answers (1)

Adam Bilinski
Adam Bilinski

Reputation: 1198

Replace this:

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

</Grid>

with this:

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

</Grid>

Upvotes: 3

Related Questions