我想创建一个包含按钮的列表框,每次选择一个按钮(动画)我的代码是这样的:
<UserControl x:Class="sa.UserControl2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="108" d:DesignWidth="592" xmlns:my="clr-namespace:sa">
<Grid Width="572">
<ListBox Name="ListBoxV" ScrollViewer.HorizontalScrollBarVisibility="Hidden"
Height="96" HorizontalAlignment="Left" VerticalAlignment="Top"
Width="572"
ItemsSource="{Binding ListItems}" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Padding" Value="30 0 30 0" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</Grid>
</UserControl>
public UserControl2()
{
InitializeComponent();
List<Temp> ListItems = new List<Temp>();
for (int i = 0; i < 20; i++)
{
ListItems.Add(new Temp("a"+i));
}
ListBoxV.ItemsSource = ListItems;
DispatcherTimer dt = new DispatcherTimer();
dt.Tick += new EventHandler(dt_Tick);
dt.Interval = TimeSpan.FromSeconds(2);
dt.Start();
}
void dt_Tick(object sender, EventArgs e)
{
if ((ListBoxV.SelectedIndex + 1) < ListBoxV.Items.Count)
ListBoxV.SelectedIndex = ListBoxV.SelectedIndex + 1;
else
ListBoxV.SelectedIndex = 0;
ListBoxV.ScrollIntoView(ListBoxV.SelectedItem);
}
public class Temp
{
public Temp(string s)
{Button b = new Button();
b.Name=s; }
}
}
列表框不显示按钮,只有动画在为每个元素添加“sa.UserControl2.Temp”。
当显示列表的末尾时,它想回到列表的开头。