Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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?

C#
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);
            }
        }
   }
}


What I have tried:

i tried different appoaches of the foreach but cant get it to work
Posted
Updated 6-Mar-19 22:55pm
Comments
[no name] 5-Mar-19 4:27am    
You haven't explained what the point of all this is. Few people are going to be able to give you their best advice. Indicators are that an on-the-fly "Buttons on tabs" is not an ideal solution for whatever you may be attempting.

And your constructor is too busy. Most / all of this should be happening in your "Loaded" event handler.
Richard Deeming 5-Mar-19 8:20am    

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

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
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
<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>
 
Share this answer
 
v3
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

C#
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

<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>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900