WPF后台自动添加控件Demo

xaml

<Window x:Class="EBPlugIn2.EBPlugIn2_YJW_13"
             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" 
             xmlns:local="clr-namespace:EBPlugIn2"
             mc:Ignorable="d" Title=""
             Height="300" Width="500">
    <Grid x:Name="mainGrid">
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition Height="30"/>
            
        </Grid.RowDefinitions>
        <Button Grid.Row="1" x:Name="okBtn" Content="确定" Width="60" Click="okBtn_Click"/>
    </Grid>
</Window>

cs

using EBCore;
using GuiDB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace EBPlugIn2
{
    /// <summary>
    /// EBPlugIn2_YJW_13.xaml 的交互逻辑
    /// </summary>
    public partial class EBPlugIn2_YJW_13 : Window
    {
        public EBPlugIn2_YJW_13()
        {
            InitializeComponent();
            createRadioBtns();
        }

        private void createRadioBtns()
        {
            
            EBD EbD = EBD.Instan;
            List<View> views = EbD.ommView;
            List<string> viewNames = (from v in views
                                      select v.ViewName).ToList();
            //动态创建单选框
            StackPanel stackPanel = new StackPanel();
            for(int i = 0; i < viewNames.Count; i++)
            {
                RadioButton radioBtn = new RadioButton();
                radioBtn.Content = viewNames[i];
                radioBtn.GroupName = "viewRadios";
                radioBtn.Checked += RadioBtn_Checked;
                stackPanel.Children.Add(radioBtn);
            }
            ScrollViewer scroller = new ScrollViewer();
            scroller.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
            scroller.Content = stackPanel;
            Grid.SetRow(scroller, 0);
            mainGrid.Children.Add(scroller);
        }

        private void RadioBtn_Checked(object sender, RoutedEventArgs e)
        {
            
        }

        private void okBtn_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}

posted @ 2023-07-10 14:55  JohnYang819  阅读(111)  评论(0编辑  收藏  举报