WPF布局间的切换方法
效果图,两种效果间的切换
xaml:
<StackPanel> <Button Content="wrap布局" Click="Button_Click" Width="100"></Button> <Button Content="stack布局" Click="Button_Click_1" Width="100"></Button> <ListBox Name="lb" HorizontalContentAlignment="Stretch"> <ListBox.ItemTemplate> <DataTemplate> <Border BorderThickness="1" BorderBrush="Red"> <TextBlock Text="{Binding }"></TextBlock> </Border> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </StackPanel>
后台事件:
public MainWindow() { InitializeComponent(); List<string> list = new List<string>(); for (int i = 0; i < 10; i++) { list.Add("item" + i); } lb.ItemsSource = list; } private void Button_Click(object sender, RoutedEventArgs e) { lb.ItemsPanel = this.FindResource("one") as ItemsPanelTemplate; } private void Button_Click_1(object sender, RoutedEventArgs e) { lb.ItemsPanel = this.FindResource("two") as ItemsPanelTemplate; } }