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?