制作我的第一个WPF应用程序,我遇到了自己创建的自定义用户控件的问题。自定义控件是一个小小的足球衬衫,由路径和两个标签组成,一个用于名称,一个用于数字。最终,用户将使用此控件调整团队的形成,但填充颜色应与所选团队匹配。我为填充/画笔设置了一个依赖属性,我希望我可以在我的XAML中进行编辑,但无论我设置什么颜色,填充都是透明的。我究竟做错了什么?这是制作这种控件的最佳方式,我可以将画布/拖放到其他面板上以交换数据。这是代码:
PlayerIcon.xaml -
<UserControl x:Class="PlayerIcon"
x:Name="PlayerIcon"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:Custom="clr-namespace:RowZedPlus"
mc:Ignorable="d"
d:DesignHeight="60" d:DesignWidth="100">
<Grid x:Name="LayoutRoot" HorizontalAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="19"></RowDefinition>
<RowDefinition Height="0"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Label Grid.Row="0" HorizontalContentAlignment="Center" Background="Transparent" Foreground="White">
<TextBlock Name="PlayerName" Foreground="White" FontSize="8">
HELLO
</TextBlock>
</Label>
<Canvas Margin="0,0,40,0" HorizontalAlignment="Center" Grid.Row="1">
<!-- Jersey/Main Shirt -->
<Path x:Name="PlayerShirt" Stroke="#ff000000" Fill="{TemplateBinding Custom:PlayerIcon.PlayerColour}" StrokeMiterLimit="1.0" Data="F1 M 0.899,9.143 C 0.899,9.143 3.484,14.188 3.984,17.000 C 6.484,15.562 9.984,15.562 9.984,15.562 L 9.984,37.625 C 9.984,37.625 25.297,40.438 31.672,37.625 C 31.609,29.250 31.672,15.562 31.672,15.562 C 31.672,15.562 35.922,15.812 37.234,16.812 C 37.234,16.812 37.554,12.941 40.421,9.253 L 24.386,3.357 C 23.526,6.286 20.586,8.812 20.586,8.812 C 20.586,8.812 18.047,6.857 16.922,3.500 C 16.922,3.500 2.790,8.143 0.899,9.143 Z">
</Path>
<!-- Jersey/Left Sleeve -->
<Path StrokeThickness="0.5" Stroke="#ff000000" StrokeMiterLimit="1.0" Fill="White" Data="F1 M 1.484,9.000 C 1.484,9.000 4.109,15.230 5.984,16.168 L 3.734,17.250 C 3.734,17.250 0.250,13.188 0.250,9.375 L 1.484,9.000 Z"/>
<!-- Jersey/Right Sleeve -->
<Path StrokeThickness="0.5" Stroke="#ff000000" StrokeMiterLimit="1.0" Fill="White" Data="F1 M 39.688,9.000 C 39.688,9.000 37.062,15.230 35.188,16.168 L 37.438,17.250 C 37.438,17.250 40.922,13.188 40.922,9.375 L 39.688,9.000 Z"/>
<!-- Jersey/Left Collar -->
<Path StrokeThickness="0.5" Stroke="#ff000000" StrokeMiterLimit="1.0" Fill="White" Data="F1 M 16.922,3.500 L 14.711,4.244 C 14.711,4.244 16.672,9.438 18.109,10.500 L 20.586,8.812 C 20.586,8.813 17.109,5.875 16.922,3.500 Z"/>
<!-- Jersey/Right Collar -->
<Path StrokeThickness="0.5" Stroke="#ff000000" StrokeMiterLimit="1.0" Fill="White" Data="F1 M 24.386,3.357 L 26.504,4.136 C 26.504,4.136 24.636,9.294 23.198,10.357 L 20.734,8.750 C 20.734,8.750 24.198,5.732 24.386,3.357 Z"/>
<!-- Jersey/Bottom Shirt -->
<Path StrokeThickness="0.5" Stroke="#ff000000" StrokeMiterLimit="1.0" Fill="White" Data="F1 M 9.984,36.875 C 17.172,38.688 25.609,38.812 31.734,36.875 L 31.734,37.938 C 25.922,40.312 18.160,40.312 9.984,37.938 C 9.984,37.938 9.984,37.188 9.984,36.875 Z"/>
<!-- Jersey/<Path> -->
<Path StrokeThickness="0.5" Stroke="#fffb02ef" StrokeMiterLimit="1.0" Data="F1 M 7.422,45.375"/>
<Label HorizontalContentAlignment="Center" Background="Transparent" Foreground="#FFED0606">
<TextBlock Name="PlayerNumber" Margin="5,5,0,0" Foreground="White" FontSize="20" HorizontalAlignment="Center" VerticalAlignment="Center">
</TextBlock>
</Label>
</Canvas>
</Grid>
PlayerIcon.xaml.vb -
Partial Public Class PlayerIcon
Public Shared ReadOnly PlayerColourProperty As DependencyProperty = _
DependencyProperty.Register("PlayerColour", _
GetType(Brush), GetType(PlayerIcon), _
New FrameworkPropertyMetadata(Brushes.Red))
Public Property PlayerColour() As Brush
Get
Return GetValue(PlayerColourProperty)
End Get
Set(ByVal value As Brush)
SetValue(PlayerColourProperty, value)
End Set
End Property
End Class
MainWindow.xaml -
<Custom:PlayerIcon PlayerColour="Green"></Custom:PlayerIcon>
<Custom:PlayerIcon Canvas.Left="300" PlayerColour="Purple"></Custom:PlayerIcon>
答案 0 :(得分:1)
您希望为此使用RelativeSource
绑定。
作为TemplateBinding州的文档,您只能在ControlTemplate
定义中使用它。
尝试将Fill="{TemplateBinding Custom:PlayerIcon.PlayerColour}"
切换为
Fill="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Custom:PlayerIcon}},
Path=PlayerColour}"
你在这条UserControl
的正确道路AFAIK w.r.t。你已经抽象了控件。您现在可以扩展功能并相应地将它们绑定到相应的位。
在旁注:
漂亮的足球运动衫Path
btw :)看起来不错