动态tabcontrol与动态按钮 [英] Dynamic tabcontrol with dynamic buttons

查看:67
本文介绍了动态tabcontrol与动态按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是我在每个标签项中只获得1个按钮。我得到特定文件夹中每个文件的tabitem。而不是只有几个标题



目录中的声音,有5个文件夹,(设置1-5)这些我想要的tabitem.header。在这些文件夹中我想要按钮链接到设置1-5中的文件



我希望问题很清楚。我非常喜欢c#编码。对我糟糕的英语道歉。



可以帮助我们在这里遇到一些帮助吗?



the problem is that i only get 1 button in each tab item. and i get a tabitem for each file in the specific folder. instead of only a couple of headers

in the directory sounds, there are 5 folders, (set 1-5) these i want as tabitem.header. and within these folders i want buttons linked to the files within set 1-5

i hope in problem is clear. i am faily new to c# coding. ad my apologies for my bad english.

can some1 plz help met out here?

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


        string path1 = @"d:\\root\\sounds\\";   /*Environment.GetFolderPath(Environment.SpecialFolder.Desktop);*/
        string[] files = Directory.GetFiles(path1, "*.*", SearchOption.AllDirectories);
        string[] dir = Directory.GetDirectories(path1);
        string result;





        foreach (string sFile in files)
        { 
            Button nwbut = new Button();
            result = System.IO.Path.GetFileNameWithoutExtension(sFile);
            nwbut.Content = result;
            nwbut.Height = 30;
            nwbut.Width = 80;
            nwbut.Margin = Margin = new Thickness(5, 5, 5, 5);
            nwbut.Click += (s, e) => { mediaElement2.Source = new Uri(sFile); };
            WrapPanel wp = new WrapPanel();
            wp.Children.Add(nwbut);

            Grid gr = new Grid();
            gr.Children.Add(wp);
            TabItem ti = new TabItem();
            ti.Content = gr;

                foreach  (string header in dir)
                    ti.Header = System.IO.Path.GetFileNameWithoutExtension(header);
                    tc.Items.Add(ti);
            }
        }
   }
}





我尝试了什么:



i尝试了不同的foreach appoaches但是无法让它工作



What I have tried:

i tried different appoaches of the foreach but cant get it to work

推荐答案

问题是:你没有任何合适的数据模型。

此外,组装用户界面并不像你想要的那样好。请改用XAML。



让我们从SoundFile的模型开始:

SoundFile.cs

The problem is: You do not have any decent data model.
Moreover it is not a good idea to assemble the user interface like you are trying to do. Use XAML instead.

Let's start with a model for SoundFile:
SoundFile.cs
namespace DynamicTest.Models
{
    public class SoundFile
    {
        public string Filename { get; set; }
    }
}



你还需要SoundSet的模型:

SoundSet.cs


You also need a model for SoundSet:
SoundSet.cs

using System.Collections.Generic;

namespace DynamicTest.Models
{
    public class SoundSet
    {
        public string Foldername { get; set; }

        public List<SoundFile> SoundFiles { get; set; }

        public SoundSet()
        {
            SoundFiles = new List<SoundFile>();
        }
    }
}





MainWindow.xaml.cs



MainWindow.xaml.cs

using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using DynamicTest.Models;

namespace DynamicTest
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            Loaded += MainWindow_Loaded;

            InitializeComponent();
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var sourcePath = @"c:\temp\root\sounds";

            var subdirs = Directory.GetDirectories(sourcePath);

            var soundSets = new List<SoundSet>();

            foreach (var subdir in subdirs)
            {
                var soundSet = new SoundSet {Foldername = subdir};
                
                var filenames = Directory.GetFiles(subdir);

                foreach (var filename in filenames)
                {
                    soundSet.SoundFiles.Add(new SoundFile {Filename = filename});
                }

                soundSets.Add(soundSet);
            }

            MyTabControl.ItemsSource = soundSets;
            MyTabControl.SelectedIndex = 0;
        }

        private void ButtonPlay_OnClick(object sender, RoutedEventArgs e)
        {
            var button = sender as Button;

            if(button == null) return;

            var filename = button.Tag as string;

            // Here you can do whatever you want with the filename
        }
    }
}





MainWindow.xaml



MainWindow.xaml

<Window x:Class="DynamicTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:DynamicTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TabControl Name="MyTabControl">
            <TabControl.ItemTemplate>
                <DataTemplate>
                   <TextBlock Text="{Binding Foldername}"></TextBlock>
                </DataTemplate>
            </TabControl.ItemTemplate>
            <TabControl.ContentTemplate>
                <DataTemplate>
                   <DataGrid ItemsSource="{Binding SoundFiles}"
                             AutoGenerateColumns="False"
                             CanUserAddRows="False">
                        <DataGrid.Columns>
                            <DataGridTemplateColumn>
                                <DataGridTemplateColumn.HeaderTemplate>
                                    <DataTemplate>
                                        <TextBlock Text=""></TextBlock>
                                    </DataTemplate>
                                </DataGridTemplateColumn.HeaderTemplate>
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <Button Name="ButtonPlay"
                                                Width="75"
                                                Height="25"
                                                Margin="5"
                                                Content="Play"
                                                Tag="{Binding Filename}"
                                                Click="ButtonPlay_OnClick"></Button>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTemplateColumn Width="*">
                                <DataGridTemplateColumn.HeaderTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="Filename"></TextBlock>
                                    </DataTemplate>
                                </DataGridTemplateColumn.HeaderTemplate>
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding Filename}"></TextBlock>
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>
                </DataTemplate>
            </TabControl.ContentTemplate>
        </TabControl>
    </Grid>
</Window>


同时我在等待代码的帮助,我将我的代码改为工作代码。现在我看到它不是程序员通常编码的方式。但我的工作与预期相符。



你可以看一下并评论吗?



mainwindow.xaml。 cs



meanwhile i was waiting for help on my code, i altered my code to a working code. now i see that it is not the way programmers usually code. but i works like intended.

can you take a look at it and comment it?

mainwindow.xaml.cs

using System.IO;
using System.Windows;
using System.Windows.Controls;

namespace reflex_scherm
{
   
    public partial class MainWindow : Window
    {
        
        public MainWindow()
        {
            InitializeComponent();

            string path1 = @"d:\root\sounds\";   
            string[] dir = Directory.GetDirectories(path1);
            string result;
            foreach (string header in dir)
            {
                TabItem ti = new TabItem();
                Grid gr = new Grid();
                WrapPanel wp = new WrapPanel();
                result = Path.GetFileNameWithoutExtension(header);
                string[] files = Directory.GetFiles(path1 + result, "*.*", SearchOption.AllDirectories);
               
                
                ti.Header = Path.GetFileNameWithoutExtension(header);
                tc.Items.Add(ti);
                ti.Content = gr;
                gr.Children.Add(wp);
                foreach (string file in files)
                {
                    string filenaam1 = Path.GetFileNameWithoutExtension(file);
                    Button btn = new Button();
                    btn.Content = filenaam1;
                    btn.Height = 30;
                    btn.Width = 120;
                    btn.Margin = Margin = new Thickness(5, 5, 5, 5);
                    btn.Click += (s, e) => { mediaElement2.Source = new System.Uri(file);
                        mediaElement2.Play();
                    };
                    wp.Children.Add(btn);

                }
                Button btn1 = new Button();
                btn1.Content = "stop";
                btn1.Height = 30;
                btn1.Width = 80;
                btn1.HorizontalAlignment = HorizontalAlignment.Right;
                btn1.VerticalAlignment = VerticalAlignment.Bottom;
                gr.Children.Add(btn1);
                btn1.Click += (s, e) => { mediaElement2.Pause(); };
            }
    
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.MainWindow.Close();
        }
    }
}







mainwindow .xaml






mainwindow.xaml

<pre lang="c#"><Window x:Class="reflex_scherm.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:reflex_scherm"
        mc:Ignorable="d"
        Title="MainWindow" Height="1080" Width="1920" WindowState="Maximized" WindowStyle="None">
    <Window.Resources>
        <Color x:Key="punten">Black</Color>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0*" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <TabControl x:Name="tc" HorizontalAlignment="Right" Height="463" Margin="0,0,-0.4,0" VerticalAlignment="Top" Width="293" Grid.ColumnSpan="2">
           </TabControl>
        <MediaElement x:Name="mediaElement2" HorizontalAlignment="Left" Height="100" Margin="1708,964,0,0" VerticalAlignment="Top" Width="100" LoadedBehavior="Manual" UnloadedBehavior="Stop" Grid.ColumnSpan="2" />
        <Button x:Name="button" Grid.ColumnSpan="2" Content="exit program" HorizontalAlignment="Right" Margin="0,10,10,10" VerticalAlignment="Bottom" Width="75" Click="button_Click"/>
    </Grid>
</Window>


这篇关于动态tabcontrol与动态按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆