0
votes

I have been making my own custom control and I ran into trouble when I wanted to my OwnButton to react as proper component. I would like my component's content be "ownButton" when it is dragged to designer and then there would be a property "Content" in property-list where a programmer would be able to change that text to whatever is needed.

Here is a part of my Generic.xaml file:

 <Button x:Name="PART_Button" Grid.Row="1" Margin="1,1,1,1" Template="{StaticResource OwnButtonTemplate}">
      <TextBlock Text="ownButton" FontFamily="Tahoma" FontSize="10"  HorizontalAlignment="Center" VerticalAlignment="Center" Name="textBlock" />
 </Button>

I was able to make a DependencyProperty in OwnButton class, but it isn't linked to Button's content. Here is the code:

private const string defaultContent = "ownButton";

 public String Content
 {
        get { return (String)GetValue(ContentProperty); }

        set { SetValue(ContentProperty, value); }

 }

 public static readonly DependencyProperty ContentProperty =

 DependencyProperty.Register("Content", typeof(String), typeof(OwnButton), new    FrameworkPropertyMetadata(OwnButton.defaultContent, new PropertyChangedCallback(OwnButton.OnContentChanged), new CoerceValueCallback(OwnButton.CoerceContent)));

Would someone be able to help me with my problem?

1
What is exactly the problem? You don't see a property named "Content" in the designer's properties? Because I copied the code and put the component in a WPF window, and I DO see this property. So I believe there is a problem with your VS. - Alex Shtof
And if your problem is the linking of the textblock's text to the content of the control - say so and I will write a proper answer with a well-formatted code. - Alex Shtof
Yes, my problem is exactly that you explained. I would like to link textblock's text to the content of the control. I would be grateful for that code. - GC87

1 Answers

0
votes

It is not possible to bind to component's own DependencyProperty. However, finally I found a solution when I used the fact that the component is a Button with a TextBlock in it. My solution doesn't work on the designer as you can guess from the code, but does what it should do when the program is executed.

 <Button x:Name="PART_Button" Grid.Row="1" Margin="1,1,1,1" Template="{StaticResource ButtonRandomTemplate}">
                            <TextBlock Text="{Binding Content, ElementName=PART_Button, UpdateSourceTrigger=PropertyChanged}" FontFamily="Tahoma" FontSize="10"  HorizontalAlignment="Center" VerticalAlignment="Center" Name="textBlock" />

I used databinding to the TextBlock's Text-property and now I was able to combine my component's DependencyProperty Content and PART_Button in code.