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


        }
    }

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

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

4

2 回答 2

0

要在 ListBox 中显示按钮,您必须为其编写 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 属性的文本。

于 2013-02-17T12:36:57.853 回答
0

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

首先,您用来填写 ListBox (或任何其他ItemsControl基于元素)的项目严格来说应该是数据项目。这些不能包含Buttons 之类的东西。最好在 WPF 中保持应用程序逻辑和 UI 分离。

下面是一个带有 ItemTemplate 的 ListBox 示例:

<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}"/>
于 2013-02-17T12:44:03.940 回答