开发者

wpf DependencyProperty for button custom control

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" />
 &l开发者_Python百科t;/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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜