1

我的第一WPF應用程序,我有與我創建了一個自定義用戶控件的問題。自定義控件是一個用路徑和兩個標籤製作的小足球襯衫,一個用於名稱和一個用於編號。最終用戶將使用此控件調整團隊的陣型,但填充顏色應與所選隊伍匹配。我已經爲fill/brush設置了一個依賴項屬性,我希望我可以在我的XAML中進行編輯,但無論我設置了什麼顏色,填充都是透明的。我究竟做錯了什麼?這是進行這種控制的最佳方式,我可以將畫布拖放到其他面板上以交換數據。下面是代碼:WPF VB - TemplateBinding填寫上自定義用戶控件和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> 

回答

1

你想要使用RelativeSource爲此綁定。

TemplateBinding

由於文檔指出,你只能在ControlTemplate定義使用它。

嘗試切換到Fill="{TemplateBinding Custom:PlayerIcon.PlayerColour}"

Fill="{Binding RelativeSource={RelativeSource FindAncestor, 
               AncestorType={x:Type Custom:PlayerIcon}}, 
       Path=PlayerColour}" 

您是在正確的道路上AFAIK w.r.t這個UserControl。你已經把控制抽象出來了。您現在可以擴展功能並相應地將它們連接到相應的位。

一個側面說明

尼斯足球球衣Path BTW :)看起來不錯

+0

非常感謝您的回覆,我會試試這個明天上午,讓你知道我是怎麼進行。附:我用了一個Illustrator來製作XAML轉換器! – rwmck 2013-04-29 20:21:42

+0

@RyanMcKenna你的歡迎的。你知道你可以只使用Expression Blend中導入的Illustrator * .AI文件到您的項目嗎?你不需要外部轉換器。 Blend還將所有畫板保留爲Path子項的Canvas項目。這就是我們所做的;) – Viv 2013-04-29 23:24:12

+0

這工作得很好!嘿,謝謝你所有的好建議。真的很感激。 – rwmck 2013-04-30 08:36:48