在 ControlTemplate 中的样式中使用 AttachedProperty
这是我的简单应用程序:
<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"
提前致谢!
Here is my simple application:
<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>
Here is my 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}"
this of course doesn't work. so how can I get value from here app:Extras.CornerRadius="10"
thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
TemplateBinding
而不是Binding
:好的,然后尝试一下:
Use a
TemplateBinding
rather than aBinding
:OK, try that then :