这是我的简单应用程序:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:app="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<Style x:Key="Test">
<Setter Property="Button.Template">
<Setter.Value>
<ControlTemplate>
<Border BorderBrush="Blue"
BorderThickness="3"
Background="Black"
CornerRadius="{Binding app:Extras.CornerRadius}"
>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Window.Resources>
<Grid>
<Button Height="23"
HorizontalAlignment="Left"
Margin="29,26,0,0"
Name="button1"
VerticalAlignment="Top" Width="75"
app:Extras.CornerRadius="10"
Style="{StaticResource Test}"
>Button</Button>
</Grid>
</Window>
这是我的AttachedProperty:
namespace WpfApplication1
{
public class Extras
{
public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.RegisterAttached(
"CornerRadius",
typeof(double),
typeof(Button),
new FrameworkPropertyMetadata(1.0d, FrameworkPropertyMetadataOptions.AffectsRender)
);
public static void SetCornerRadius(UIElement element, double value)
{
element.SetValue(CornerRadiusProperty, value);
}
public static double GetCornerRadius(UIElement element)
{
return (double)element.GetValue(CornerRadiusProperty);
}
}
}
CornerRadius="{Binding app:Extras.CornerRadius}"
这当然不起作用。那么我怎样才能从这里获得价值app:Extras.CornerRadius="10"
提前感谢!
答案 0 :(得分:2)
使用TemplateBinding
而不是Binding
:
<Border BorderBrush="Blue"
BorderThickness="3"
Background="Black"
CornerRadius="{TemplateBinding app:Extras.CornerRadius}"
>
好的,那就试试吧:
<Border BorderBrush="Blue"
BorderThickness="3"
Background="Black"
CornerRadius="{Binding (app:Extras.CornerRadius), RelativeSource={x:Static RelativeSource.TemplatedParent}}"
>