0

我在每个按钮下方创建带有扩展器的动态按钮。单击每个按钮时,我希望此按钮下方的扩展器将打开。我如何知道在每个按钮中单击哪个扩展器打开?

public partial class MainWindow : Window
{

    List<List<string>> Buttons;


    public MainWindow()
    {
        InitializeComponent();

        Buttons = new List<List<string>>();

        /////////////////////////////////////////
        List<string> lst1 = new List<string>();
        lst1.Add("main1");
        lst1.Add("a1");
        lst1.Add("a2");
        lst1.Add("a3");
        lst1.Add("a4");
        Buttons.Add(lst1);
        /////////////////////////////////////////
        List<string> lst2 = new List<string>();
        lst2.Add("main2");
        lst2.Add("b1");
        lst2.Add("b2");
        lst2.Add("b3");
        lst2.Add("b4");
        Buttons.Add(lst2);
        /////////////////////////////////////////
        List<string> lst3 = new List<string>();
        lst3.Add("main3");
        lst3.Add("c1");
        lst3.Add("c2");
        lst3.Add("c3");
        lst3.Add("c4");
        Buttons.Add(lst3);

        for (int i = 0; i < Buttons.Count; i++)
        {
            Button newBtn = new Button();
            newBtn.Content = Buttons[i][0];
            newBtn.Name = "Button" + i.ToString();

            newBtn.Height = 23;
            stackPanel1.Children.Add(newBtn);
            newBtn.Click += new RoutedEventHandler(newBtn_Click);


            Expander expader = new Expander();
            StackPanel newStck = new StackPanel();

            for (int j = 1; j < Buttons[i].Count; j++)
            {
                Button newBtnIn = new Button();
                newBtnIn.Content = Buttons[i][j];
                newBtnIn.Name = "Button" + j.ToString();
                newBtnIn.Height = 23;
                newBtnIn.Width = 100;

                newStck.Children.Add(newBtnIn);
            }

            expader.Content = newStck;
            stackPanel1.Children.Add(expader);
        }


    }

    private void newBtn_Click(object sender, RoutedEventArgs e)
    {
         //open specific expander below button
    }
}
4

2 回答 2

1

您可以使用按钮的 Tag 属性为按钮提供对其扩展器的引用。一个可行的解决方案是:

 public partial class MainWindow : Window
{

    List<List<string>> Buttons;


    public MainWindow()
    {
        InitializeComponent();

        Buttons = new List<List<string>>();

        /////////////////////////////////////////
        List<string> lst1 = new List<string>();
        lst1.Add("main1");
        lst1.Add("a1");
        lst1.Add("a2");
        lst1.Add("a3");
        lst1.Add("a4");
        Buttons.Add(lst1);
        /////////////////////////////////////////
        List<string> lst2 = new List<string>();
        lst2.Add("main2");
        lst2.Add("b1");
        lst2.Add("b2");
        lst2.Add("b3");
        lst2.Add("b4");
        Buttons.Add(lst2);
        /////////////////////////////////////////
        List<string> lst3 = new List<string>();
        lst3.Add("main3");
        lst3.Add("c1");
        lst3.Add("c2");
        lst3.Add("c3");
        lst3.Add("c4");
        Buttons.Add(lst3);

        for (int i = 0; i < Buttons.Count; i++)
        {
            Button newBtn = new Button();
            newBtn.Content = Buttons[i][0];
            newBtn.Name = "Button" + i.ToString();

            newBtn.Height = 23;
            stackPanel1.Children.Add(newBtn);
            newBtn.Click += new RoutedEventHandler(newBtn_Click);


            Expander expader = new Expander();
            StackPanel newStck = new StackPanel();

            for (int j = 1; j < Buttons[i].Count; j++)
            {
                Button newBtnIn = new Button();
                newBtnIn.Content = Buttons[i][j];
                newBtnIn.Name = "Button" + j.ToString();
                newBtnIn.Height = 23;
                newBtnIn.Width = 100;
                newBtn.Tag = expader;
                newBtn.Click+=newBtn_Click;
                newStck.Children.Add(newBtnIn);
            }

            expader.Content = newStck;
            stackPanel1.Children.Add(expader);
        }


    }

    private void newBtn_Click(object sender, RoutedEventArgs e)
    {
        Button b = sender as Button;

        if (b == null)
            return;

        Expander ex = b.Tag as Expander;

        if (ex == null)
            return;

        ex.IsExpanded = !ex.IsExpanded;
    }
}
于 2013-10-22T12:52:17.907 回答
0

我想您应该使用它ToggleButton来代替Button它来处理IsChecked状态并因此展开/折叠底层Expander控件。
因此,您将能够定义DataTemplatewithToggleButtonExpanderinside:

<DataTemplate>
   <StackPanel>
     <ToggleButton x:Name="expandButton" IsChecked="false"/>
     <Expander IsExpanded="{Binding IsChecked, ElementName=expandButton}">
        <TextBlock TextWrapping="Wrap">
             Lorem ipsum dolor sit amet, consectetur
             adipisicing elit, sed do eiusmod tempor incididunt ut
             labore et dolore magna aliqua
        </TextBlock>
     </Expander>
   </StackPanel>
</DataTemplate>

之后,您应该考虑使用其中一个ListViewListBox任何其他合适的控件,ItemsSource并将其设置为您的按钮列表。这将为您提供干净的设计,而无需大量代码隐藏,这不如实践显示的那么好。

于 2013-10-22T12:52:56.937 回答