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);
//}
//}
}