0
我已經創建了幾個簡單的形狀,例如矩形,圓形以及能夠將它們放置在工具欄上,因此可以拖放到Canvas上。但是,我遇到了由4個矩形和線組成的WPF對象的顯示問題。爲了方便起見,我使用了blend 4來創建用戶控件,並且現在將下面的XAML標籤作爲用戶控件。在WPF的工具欄上顯示用戶控件
問題:如何在現有工具欄上顯示此形狀(矩形和線條的組合)?
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
x:Class="test1.UserControl2"
x:Name="connector1"
d:DesignWidth="100" d:DesignHeight="90">
<Grid x:Name="LayoutRoot">
<Rectangle Fill="#FF1B1BCE" Margin="41.334,9.833,42.666,14.167" Stroke="Black"/>
<Rectangle Fill="#FF1B1BCE" HorizontalAlignment="Left" Margin="33.334,29,0,30.5" Stroke="Black" Width="8"/>
<Rectangle Fill="#FF1B1BCE" HorizontalAlignment="Right" Height="8.833" Margin="0,9.833,36.499,0" Stroke="Black" VerticalAlignment="Top" Width="6.167"/>
<Rectangle Fill="#FF1B1BCE" HorizontalAlignment="Right" Height="8.833" Margin="0,0,36.499,14.167" Stroke="Black" VerticalAlignment="Bottom" Width="6.167"/>
<Path Data="M62.083333,37 L70.416667,37" Fill="#FFEFEFF8" HorizontalAlignment="Right" Height="1" Margin="0,29,24.749,0" Stretch="Fill" Stroke="Black" VerticalAlignment="Top" Width="17.917"/>
<Path Data="M62.083333,37 L70.416667,37" Fill="#FFEFEFF8" HorizontalAlignment="Right" Height="1" Margin="0,34,24.749,0" Stretch="Fill" Stroke="Black" VerticalAlignment="Top" Width="17.917"/>
<Path Data="M62.083333,37 L70.416667,37" Fill="#FFEFEFF8" HorizontalAlignment="Right" Height="1" Margin="0,39,24.749,0" Stretch="Fill" Stroke="Black" VerticalAlignment="Top" Width="17.917"/>
<Path Data="M62.083333,37 L70.416667,37" Fill="#FFEFEFF8" HorizontalAlignment="Right" Height="1" Margin="0,44,24.749,0" Stretch="Fill" Stroke="Black" VerticalAlignment="Top" Width="17.917"/>
<Path Data="M62.083333,37 L70.416667,37" Fill="#FFEFEFF8" HorizontalAlignment="Right" Height="1" Margin="0,0,24.749,40" Stretch="Fill" Stroke="Black" VerticalAlignment="Bottom" Width="17.917"/>
<Path Data="M62.083333,37 L70.416667,37" Fill="#FFEFEFF8" HorizontalAlignment="Right" Height="1" Margin="0,0,24.749,35" Stretch="Fill" Stroke="Black" VerticalAlignment="Bottom" Width="17.917"/>
<Path Data="M62.083333,37 L70.416667,37" Fill="#FFEFEFF8" HorizontalAlignment="Right" Height="1" Margin="0,0,24.749,30.5" Stretch="Fill" Stroke="Black" VerticalAlignment="Bottom" Width="17.917"/>
<Path Data="M62.083333,37 L70.416667,37" Fill="#FFEFEFF8" HorizontalAlignment="Right" Height="1" Margin="0,0,24.749,25.5" Stretch="Fill" Stroke="Black" VerticalAlignment="Bottom" Width="17.917"/>
<Path Data="M62.083333,37 L70.416667,37" Fill="#FFEFEFF8" HorizontalAlignment="Right" Height="1" Margin="0,24,24.749,0" Stretch="Fill" Stroke="Black" VerticalAlignment="Top" Width="17.917"/>
</Grid>
我很欣賞你的反應。當然,我現在可以在工具欄(空白方塊)上看到一個空白區域,沒有任何邊框,就像你建議的那樣,我嘗試了,但是沒有邊界在運行時顯示,因爲它沒有邊界不適用於主機級別,然後我在創建用戶控件但同樣的事情的XAML中嘗試它....任何想法爲什麼它是空的,也不能添加邊界到它?我在這裏做錯了事。 –
在UserControl中,邊框的屬性是BorderThickness和BorderBrush。寫邊框=「黑色」不應該做任何事情。如果你想添加一個MouseOver邊框,我會在我的帖子後面添加一個例子。 –
爲了使UserControl正確顯示,您需要通過在XAML中設置寬度和高度來手動設置大小,或者在UserControl的代碼隱藏中手動設置大小,您應該覆蓋MeasureOverride並將您希望的大小返回是。您只設置不影響運行時的DesignWidth和DesignHeight。 –