2013-02-17 56 views
0

我想创建一个包含按钮的列表框,每次一个按钮被选中(动画)我的代码是这样的:创建一个包含按钮列表框,并定期一个按钮被选中(一种旋转木马)

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


     } 
    } 

列表框不显示按钮,只有动画工作 affich“sa.UserControl2.Temp”为每个元素。

当显示列表的末尾时,它要回到列表的开头。

+0

@ChrisF现在 – sansa 2013-02-17 12:15:32

回答

0

这里有几个问题。我建议你在DataTemplatesMVVM

首先读了,你使用的项目填写列表框(或任何其他ItemsControl基础的元素)应该是数据项,stricly。这些不能包含诸如Button之类的东西。在WPF中维护应用程序逻辑和UI是更好的选择。

这里是一个ItemTemplate你的列表框的例子:

<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.ItemTemplate> 
     <DataTemplate> 
      <Button Content="{Binding Property}"/> 
     </DataTemplate> 
</ListBox> 

数据项:

public class Temp 
{ 
    string Property {get;set;} 

    public Temp(string s) 
    { 
     Property = s; 
    } 
} 

还要注意的是,为了当您在更改这些属性,以支持在UI更新类Temp,它将不得不实现System.ComponentModel.INotifyPropertyChanged接口。

然后,如果要执行单击每个项目的按钮时的动作,你将不得不使用ICommand

public class Temp 
{ 
    string Property {get;set;} 

    public Temp(string s) 
    { 
     Property = s; 
     DoSomethingCommand = new DelegateCommand(x => DoSomething()); 
    } 

    public DelegateCommand DoSomethingCommand {get;set;} 

    private void DoSomething() 
    { 
     //DoSomething when the Button is Clicked!! 
    } 
} 

XAML:

<Button Content="{Binding Property}" Command="{Binding DoSomethingCommand}"/> 
+0

非常感谢,非常有用 – sansa 2013-02-17 13:21:48

+0

@sansamed请标记我的答案为接受,如果它帮助你=) – 2013-02-17 13:36:21

0

要在列表框中显示按钮,您必须为其编写ItemTemplate。它看起来像这样的:

<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.ItemTempate> 
    <DataTemplate> 
      <Button Content="{Binding Property}"/> 
    </DataTemplate> 
</ListBox.ItemTempate> 

接下来,你不需要的按钮添加到您的ListBox的项集合,字符串会就好了。因此,您的类Temp需要更改为:

class Temp{ 
    string Property {get;set;} 
    public Temp(string s){ 
     Property = s; 
    } 
} 

通过这些更改,您将拥有带有按钮的列表框。这些按钮将显示来自Temp.Property属性的文本。

+0

谢谢,还有一件事情为动画,它没有retour到第一个索引结束,有没有解决方案 – sansa 2013-02-17 12:50:39

相关问题