WPF如何创建选项卡项目选项卡、项目、WPF

2023-09-04 04:34:27 作者:放纵° 随遇而安

选项卡项目的数量并不predetermined。我只是想创建新的标签项,然后添加新的矩形经常项目内。 我生成新的标签项(波纹管是code),但我怎么能在当前选项卡中添加矩形?

  VAR _FloorName =(从FN在db.floors选择fn.floorname).ToList();

如果(_FloorName.Count大于0)
{
    的for(int i = 0; I< _FloorName.Count;我++)
    {
        tabControl1.Items.Add(_FloorName [I]);
    }
}
 

解决方案

下面是你可以采取一种方法:

添加 电网 (或其他容器),以每个的TabItem 创建一个 矩形 ,你想要 呼叫 tabControl1.SelectedContent ,它转换为电网(或容器类型) 呼叫 grid.Children.Add(矩形) WPF项目创建 一

下面是一个完整的code样品(使用丰富的code-后面)。

MainWindow.xaml:

 <窗​​口x:类=主窗口
        的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
        的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
        标题=主窗口高度=350宽度=525>
    < StackPanel的保证金=12>
        < TabControl的名称=tabControl1高度=250/>
        <按钮内容=添加矩形点击=Button_Click
                WIDTH =90高度=25保证金=5/>
    < / StackPanel的>
< /窗>
 

MainWindow.xaml.cs:

 使用系统;
使用System.Collections.Generic;
使用System.Linq的;
使用System.Windows;
使用System.Windows.Controls的;
使用System.Windows.Media;
使用System.Windows.Shapes;

公共类地板
{
    公用建筑(字符串名称= NULL)
    {
        this.Name =名称;
    }

    公共字符串名称{;组; }
}

公共类FakeDb
{
    公开的IEnumerable<地板>地板
    {
        得到
        {
            返回新的List<地板>()
            {
                新楼(floor1),
                新楼(floor2),
                新楼(层3),
            };
        }
    }
}

公共部分类主窗口:窗口
{
    公共主窗口()
    {
        的InitializeComponent();
        InitializeTabControl();
    }

    私人无效InitializeTabControl()
    {
        变种DB =新FakeDb();
        VAR floorNames =(从FN在db.Floors选择fn.Name).ToList();

        的foreach(在floorNames串floorName)
        {
            VAR项目=新的TabItem()
            {
                标题= floorName,
                内容=新的网格()
            };
            tabControl1.Items.Add(项目);
        }
    }

    私人无效Button_Click(对象发件人,RoutedEventArgs E)
    {
        VAR随机=新的随机();
        VAR矩形=新的Rectangle()
        {
            行程= Brushes.Black,
            填写= Brushes.SkyBlue,
            宽度= 50,
            身高= 75,
            保证金=新的厚度(
                左:random.NextDouble()* 300,
                顶:random.NextDouble()* 150,
                右:0,
                底部:0)
            的Horizo​​ntalAlignment = Horizo​​ntalAlignment.Left,
            VerticalAlignment = VerticalAlignment.Top,
        };
        VAR电网=(网格)tabControl1.SelectedContent;
        grid.Children.Add(矩形);
    }
}
 

The number of tab items are not predetermined. I just want to create new tab items and then add new rectangles inside current items. I am generating new tab items(bellow is code) but how can I add rectangles in current tab?

var _FloorName = (from fn in db.floors select fn.floorname).ToList();

if (_FloorName.Count > 0)
{
    for (int i = 0; i < _FloorName.Count; i++)
    {
        tabControl1.Items.Add(_FloorName[i]);
    }
}

解决方案

Here is one approach you could take:

Add a Grid (or other container) to each TabItem when creating them Create a Rectangle, with the brush/dimensions you want Call tabControl1.SelectedContent, cast it to Grid (or your container type) Call grid.Children.Add(rectangle)

Here is a complete code sample (using copious code-behind).

MainWindow.xaml:

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Margin="12">
        <TabControl Name="tabControl1" Height="250" />
        <Button Content="Add Rectangle" Click="Button_Click"
                Width="90" Height="25" Margin="5" />
    </StackPanel>
</Window>

MainWindow.xaml.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;

public class Floor
{
    public Floor(string name = null)
    {
        this.Name = name;
    }

    public string Name { get; set; }
}

public class FakeDb
{
    public IEnumerable<Floor> Floors
    {
        get
        {
            return new List<Floor>()
            {
                new Floor("floor1"),
                new Floor("floor2"),
                new Floor("floor3"),
            };
        }
    }
}

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        InitializeTabControl();
    }

    private void InitializeTabControl()
    {
        var db = new FakeDb();
        var floorNames = (from fn in db.Floors select fn.Name).ToList();

        foreach (string floorName in floorNames)
        {
            var item = new TabItem()
            {
                Header = floorName,
                Content = new Grid(),
            };
            tabControl1.Items.Add(item);
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        var random = new Random();
        var rectangle = new Rectangle()
        {
            Stroke = Brushes.Black,
            Fill = Brushes.SkyBlue,
            Width = 50,
            Height = 75,
            Margin = new Thickness(
                left: random.NextDouble() * 300,
                top: random.NextDouble() * 150,
                right: 0,
                bottom: 0),
            HorizontalAlignment = HorizontalAlignment.Left,
            VerticalAlignment = VerticalAlignment.Top,
        };
        var grid = (Grid)tabControl1.SelectedContent;
        grid.Children.Add(rectangle);
    }
}