1

I derived ImageButton from Button class. I want to be able to set text on it using custom property.

ImageButton XAML

<Button x:Class="MyProject.ImageButton"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="50" Width="75" Template="{DynamicResource BlueButton}">
    <StackPanel>
        <TextBlock Text="{Binding Path = ImageButton.ButtonText}" TextWrapping="Wrap" Foreground="White"/>
    </StackPanel>
</Button>

ImageButton code behind

    public partial class ImageButton : Button
    { 
        public string ButtonText
        {
            get { return (string)GetValue(ButtonTextProperty); }
            set { SetValue(ButtonTextProperty, value); }
        }

        public static readonly DependencyProperty ButtonTextProperty =
            DependencyProperty.Register("ButtonText", typeof(string), typeof(ImageButton), new UIPropertyMetadata(string.Empty));

        public ImageButton()
        {
            InitializeComponent();
        }
    }

Client code:

<local:ImageButton Margin="114,15.879,96,15.878" Grid.Row="2" ButtonText="test"/>

No text is being shown on the button. For some reason the binding doesn't seem to be taking place. What am I doing wrong?

4
  • How are you binding to the button?
    – Oded
    Jun 21, 2012 at 20:44
  • Shouldn't the TextBlock binding be just "ButtonText"? If you're within the ImageButton, you should just have to bind to the property name.
    – Rich
    Jun 21, 2012 at 20:46
  • Look at the 'Client code:' at the very button. I set ButtonText property to "test".
    – Eternal21
    Jun 21, 2012 at 20:46
  • I tried the following variations and none of them works: "Binding ButtonText", "Binding Path = ButtonText", "Binding Path = ImageButton.ButtonText".
    – Eternal21
    Jun 21, 2012 at 20:48

2 Answers 2

3

You don’t have a DataContext set, so the data binding doesn’t know which source object it should bind to.

I find that the easiest way to resolve this is to give your outer control a name, and reference it using ElementName in your binding:

<Button x:Class="MyProject.ImageButton"
        x:Name="myImageButton"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="50" Width="75" Template="{DynamicResource BlueButton}">
    <StackPanel>
        <TextBlock Text="{Binding ElementName=myImageButton, Path=ButtonText}" 
                   TextWrapping="Wrap" Foreground="White"/>
    </StackPanel>
</Button>
2
  • 1
    @Eternal21: Glad to help! For the record, you can set DataContext from the code-behind instead and avoid naming your ImageButton, but I personally prefer the above approach.
    – Douglas
    Jun 21, 2012 at 21:01
  • How would I set datacontext inside code-behind? Just DataContext = this?
    – Eternal21
    Jun 21, 2012 at 21:12
0

Change this

 <TextBlock Text="{Binding Path = ImageButton.ButtonText}" 

to

   <TextBlock Text="{Binding Path = ButtonText}" 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.