A good day dear developers. My name is Danny.
This is my first post here on Stackoverflow… as much as i am fairly new to the .NET Framework.
I did search quite thoroughly through several forums, but am apparently looking with my nose.
My question is this:
I am writing a piece of script that reads out how many .txt-files there exist within a directory.
Then it creates the amount of GroupBoxes (with each a grid) as there are .txt’s through a ‘foreach’ command.
And within that same foreach-command, i use: Grid.Children.Add(control).
Per foreach-iteration there are 2 controls to be added to the generated grid.
The problem:
It does not do that… well sort of.
It only makes 2 iterations, regardless of how many .txt’s there are.
And within the Output-dialog it says: A first chance exception of type 'System.ArgumentException' occurred in PresentationCore.dll.
In case my explanation is not as clear as i meant to, here follows my script, and thanks for reading:
using System;
using System.IO;
using System.Linq;
using System.Windows;
using System.Threading;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Threading;
using System.Windows.Controls.Primitives;
namespace Notes
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// Create Objects
public TextBox tBox = new TextBox();
public Label fLabel = new Label();
public GroupBox group = new GroupBox();
public Grid groupGrid = new Grid();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Global Variables
int Increment = 1;
string Dir = "DIRECTORY_HERE";
int leftMargin = 10;
int betweenMargin = 10;
// GroupBox Initials
group.Height = this.Height - 125;
group.Margin = new Thickness(0, 75, 0, 0);
group.HorizontalAlignment = HorizontalAlignment.Left;
group.VerticalAlignment = VerticalAlignment.Top;
// Grid Initials
groupGrid.Width = leftMargin;
groupGrid.Height = group.Height;
groupGrid.Margin = new Thickness(0, 0, 0, 0);
groupGrid.HorizontalAlignment = HorizontalAlignment.Left;
groupGrid.VerticalAlignment = VerticalAlignment.Top;
// Label Initials
fLabel.Width = 260;
fLabel.Height = 28;
fLabel.HorizontalAlignment = HorizontalAlignment.Left;
fLabel.VerticalAlignment = VerticalAlignment.Top;
fLabel.Margin = new Thickness(0, 0, 0, 0);
// TextBox Intitials
tBox.Width = 100;
tBox.Height = groupGrid.Height;
tBox.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden;
tBox.HorizontalAlignment = HorizontalAlignment.Left;
tBox.VerticalAlignment = VerticalAlignment.Top;
tBox.Foreground = new SolidColorBrush(Colors.DarkCyan);
tBox.Background = new SolidColorBrush(Colors.Red);
tBox.TextWrapping = TextWrapping.NoWrap;
// Get paths of all files within given directory.
string[] Notes = Directory.GetFiles(Dir, "*.*", SearchOption.TopDirectoryOnly).ToArray();
foreach (string Note in Notes)
{
Console.WriteLine("NOTE: " + Note);
// Text settings
FileInfo fi = new FileInfo(Note);
TextReader tr = new StreamReader(fi.ToString());
tBox.Name = "tBox" + Increment.ToString();
tBox.Text = tr.ReadToEnd();
tBox.Margin = new Thickness(betweenMargin, 0, 0, 0);
// FileLabel settings
fLabel.Name = "fLabel" + Increment.ToString();
// GroupGrid settings
groupGrid.Name = "groupGrid" + Increment.ToString();
groupGrid.Children.Add(tBox); //error
groupGrid.Children.Add(fLabel); //error
// group settings
group.Width = leftMargin + (tBox.Width * Increment) + (betweenMargin * Increment);
group.Content = groupGrid;
// Increment
Increment++;
}
NotesDoc.Children.Add(group);
}
You are attempting to program a WPF application like you would a Windows Forms app. Believe me, you can accomplish your goal much easier if you actually learn some of the WPF development patterns (such as MVVM).
For example, this can be easily accomplished using an ObservableCollection holding all (not sure how you are doing this) FileInfo instances (one for each file).
You then bind a ItemsControl‘s ItemsSource property to this collection. Now, whenever you add a new FileInfo instance to the ObservableCollection the ItemsControl will add a new row and bind that row to the instance.
Of course, the default template for each row just calls .ToString() on each instance, so you’ll get a bunch of rows that contains “System.IO.FileInfo”. To show information about each FileInfo, you can add a DataTemplate to ItemsSource.ItemTemplate and add controls that bind to the public properties of the FileInfo.
It is VERY important to understand some of these basic patterns when developing WPF applications. Attempting to interact with the UI from the codebehind in the manner you are attempting is VERY HARD to do. Many of the UI patterns for WPF applications are optimized for use in XAML; attempting to interact with these (such as attached properties) from the codebehind can be extremely confusing and counterintuitive.
Here’s a simple MVVM sample I updated. To use this, create a new solution (3.5 or greater) called SimpleMVVM. Create a new class in the root of the project called ViewModel and add the following code to the file:
Next, open MainWindow.xaml and replace the xaml with the following:
(excuse me everybody for the code dump!) Compile, fix errors and run it.