2009-12-14 23 views

回答

2

看來你想是這樣的:

<UserControl x:Class="PracticeSample.MyButton" 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
<Grid> 
    <Button x:Name="button" Content="Add" HorizontalAlignment="Center" VerticalAlignment="Top"/> 
    <Line Stroke="Black" X1="0" Y1="0" HorizontalAlignment="Center" X2="{Binding ElementName=button, Path=ActualWidth}" Y2="{Binding ElementName=button, Path=ActualHeight}"/> 
</Grid> 

使用這種myButton的在當地按鈕的頁面,

編輯: 如果你想兩個控件之間畫線 請勿使用上述代碼示例,但請直接在您的頁面中嘗試此操作:

<Canvas HorizontalAlignment="Left" Margin="10"> 
    <Button x:Name="button2" Content="Add" Canvas.Left="10" Canvas.Top="5"/> 
    <Button Name="button" Content="Refresh Control" Canvas.Left="100" Canvas.Top="50"/> 
    <Line Stroke="Black" X1="{Binding Path=(Canvas.Left),ElementName=button2}" Y1="{Binding Path=(Canvas.Top), ElementName=button2}" X2="{Binding (Canvas.Left), ElementName=button}" Y2="{Binding (Canvas.Top), ElementName=button}"/> 
</Canvas> 

希望這有助於!

+0

我最終在我的代碼隱藏文件中進行繪圖,以便我可以對位置進行一些計算,而不是僅僅對元素的角落進行計算。 – 2009-12-30 02:51:14

0

定義一個模板,將按鈕和線條放置在任何你喜歡的地方,然後在Button的位置使用這個模板。

+0

對不起,我沒有指定 - 我不能使用模板,因爲我想綁定開始點和結束點。我想要在兩個任意元素之間畫一條線。 – 2009-12-14 15:16:50