WrapPanel:這個面板控件主要是通過Orientation屬性設置包含在控件是的元素從左至右或從上至下依次安排位置,當元素超過該控件邊緣時,它們將會被自動轉至下一行或列。此控件一般用於文本佈局、拾色器、圖片等。需要加載System.Windows.Controls.Toolkit.dll
WrapPanel:這個面板控件主要是通過Orientation屬性設置包含在控件是的元素從左至右或從上至下依次安排位置,當元素超過該控件邊緣時,它們將會被自動轉至下一行或列。此控件一般用於文本佈局、拾色器、圖片等。需要加載System.Windows.Controls.Toolkit.dll
Xaml代碼爲:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<toolkit:WrapPanel Height="504" HorizontalAlignment="Left" Margin="9,78,0,0" Name="wrapPanel1" VerticalAlignment="Top" Width="441" />
<Button Content="清空" Height="72" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="101" Click="button1_Click" />
<Button Click="OnAdd" Content="+1" Tag="1" Height="72" HorizontalAlignment="Left" Margin="107,0,0,0" Name="button2" VerticalAlignment="Top" Width="101" />
<Button Click="OnAdd" Content="+5" Height="72" HorizontalAlignment="Left" Margin="228,0,0,0" Name="button3" Tag="5" VerticalAlignment="Top" Width="101" />
<Button Click="OnAdd" Content="+8" Height="72" HorizontalAlignment="Left" Margin="355,0,0,0" Name="button4" Tag="8" VerticalAlignment="Top" Width="101" />
</Grid>
CS代碼爲:
Random rnd = new Random();
private void button1_Click(object sender, RoutedEventArgs e)
{//清空
this.wrapPanel1.Children.Clear();
}
private void OnAdd(object sender, RoutedEventArgs e)
{//根按按鈕的Tag屬性添加內容
int count = Int32.Parse((string)((FrameworkElement)sender).Tag);
while (count-- > 0)
{
AddItem();
}
}
private void AddItem()
{//添加
Border b = new Border()
{
Width = 100,
Height = 100,
Background = new SolidColorBrush(Color.FromArgb(255, (byte)rnd.Next(256), (byte)rnd.Next(256), (byte)rnd.Next(256))),
BorderThickness = new Thickness(2),
Margin = new Thickness(8)
};
b.BorderBrush = (SolidColorBrush)Resources["PhoneForegroundBrush"];
//b.Tap += OnTapped;
//註冊移除事件
GestureListener listener = GestureService.GetGestureListener(b);
listener.Tap += new EventHandler<GestureEventArgs>(WrapPanelSample_Tap);
wrapPanel1.Children.Add(b);
}
void WrapPanelSample_Tap(object sender, GestureEventArgs e)
{//移除事件
Border b = (Border)sender;
wrapPanel1.Children.Remove(b);
}
//void OnTapped(object sender, System.Windows.Input.GestureEventArgs e)
//{
// Border b = sender as Border;
//if (null != b)
//{
//wrapPanel.Children.Remove(b);
//}
//}
}