我为Button创建了一个附加属性,用于在Button ControlTemplate的画布中设置TextBlock Text属性,但我在Visual Studio 2013中的设计时遇到此异常。在Blend中显示一个错误框代替按钮,但是在运行时它工作正常。
这是附加属性类:
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);
}
}
这是ControlTemplate中的TextBlock:
<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"/>
这就是按钮:
<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"/>
如果不是问题,你可以给我一些想法,我怎么能在F1按键上按下这个按钮?