2

I created a attached property for a Button to set a TextBlock Text property in a canvas in Button ControlTemplate but I get this exception in design time in Visual Studio 2013. In Blend show an errorbox in place of button, but at Run time it`s working fine.

This is the attached property Class:

public class FButton
{
    public static readonly DependencyProperty TextBlockTextProperty =
           DependencyProperty.RegisterAttached("TextBlockText",
                                               typeof(string),
                                               typeof(FButton),
                                               new FrameworkPropertyMetadata(null));

    public static string GetTextBlockText(DependencyObject d)
    {
        return (string)d.GetValue(TextBlockTextProperty);
    }

    public static void SetTextBlockText(DependencyObject d, string value)
    {
        d.SetValue(TextBlockTextProperty, value);
    }
}

This is the TextBlock in ControlTemplate:

<TextBlock x:Name="F1" Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(custom:FButton.TextBlockText)}" Foreground="Black" FontSize="14" IsHyphenationEnabled="True" LineStackingStrategy="BlockLineHeight" LineHeight="14" TextAlignment="Left" TextWrapping="Wrap"   Opacity="0.895"/>

And this is the Button:

 <Button x:Name="btnF1" Template="{StaticResource TmpBtnF}" custom:FButton.TextBlockText="F1" Content="Button" Grid.Column="2" Margin="0,7,-1,618.669" Grid.Row="1"/>

And if is not a problem, can you give me some idea, how I can push this button on F1 keypress?

RobiGo
  • 95
  • 1
  • 10
  • That happens with one of my custom controls too, but I've always assumed that it was just because the Visual Studio WPF Designer is not fit for purpose. I'll be interested to see if you get any decent answers to this. +1 – Sheridan Oct 02 '14 at 13:24
  • the more interesting, I have used this method to transfer in a ControlTemplate Path object his Data to define his shape from Button, and in that case is worked perfectly... – RobiGo Oct 02 '14 at 13:37
  • You may have to [check for design time](http://stackoverflow.com/q/834283/1997232) and either skip something or do it differently. Check where you get exception (by putting breakpoints randomly, the only way to debug in `wpf`). – Sinatr Oct 02 '14 at 14:07
  • @Sintar I don't get this exception only in design mode, in run is runs well, without error... – RobiGo Oct 03 '14 at 12:58

0 Answers0