.net 在ControlTemplate的样式中使用AttachedProperty
以下是我的简单应用程序:.net 在ControlTemplate的样式中使用AttachedProperty,.net,wpf,xaml,dependency-properties,attached-properties,.net,Wpf,Xaml,Dependency Properties,Attached Properties,以下是我的简单应用程序: <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="Window
<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>
CornerRadius=“{Binding app:Extras.CornerRadius}”
这当然不起作用。那么我如何从这里获得价值呢app:Extras.CornerRadius=“10”
提前谢谢 使用
模板绑定
而不是绑定
:
<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}}"
>
在设计器中,它给出错误:“在类型“按钮”上找不到静态成员“CornerRadiusProperty”。”在运行时“无法将属性“CornerRadius”中的值转换为类型为“System.Windows.TemplateBindingExtension”的对象。在标记文件中的对象“System.Windows.Controls.ControlTemplate”处出错System.Windows.Data错误:39:BindingExpression路径错误:‘(应用程序:Extras.CornerRadius)’属性未在“对象”“按钮”(Name='button1')上找到。BindingExpression:Path=(应用程序:Extras.CornerRadius);DataItem='Button'(Name='button1');目标元素为“Border”(名称=“”);目标属性为“CornerRadius”(类型为“CornerRadius”)
<Border BorderBrush="Blue"
BorderThickness="3"
Background="Black"
CornerRadius="{Binding (app:Extras.CornerRadius), RelativeSource={x:Static RelativeSource.TemplatedParent}}"
>