Dynamic Grids

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 610 Views - Last Post: 26 September 2017 - 09:45 PM Rate Topic: -----

#1 JeremyBenson11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 10-January 14

Dynamic Grids

Posted 25 September 2017 - 07:02 PM

I'm trying to create a dynamic grid for speech in my game, but the grid isn't showing. I've got a couple of questions. Does each column definition create a dividing line, or a single column? As is 1 column definition will create 1 column, or two? The second is the issue itself, why isn't the grid visible? Basically I want the characters name to be beside their text speech.

public void character(string name, string speechText, StackPanel Output)
		{
			
			Grid grid = new Grid();
			grid.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
			grid.VerticalAlignment = System.Windows.VerticalAlignment.Top;
			
			ColumnDefinition col1 = new ColumnDefinition();
			ColumnDefinition col2 = new ColumnDefinition();
			RowDefinition row1 = new RowDefinition();
			
			grid.RowDefinitions.Add(row1);
			grid.ColumnDefinitions.Add(col1);
			
			Label lbl = new Label();
			lbl.Content = name;
			lbl.Padding = new System.Windows.Thickness(0);
			TextBlock speech = new TextBlock();
			speech.Padding = new System.Windows.Thickness(0);
			speech.Text = speechText;
			speech.TextWrapping = System.Windows.TextWrapping.Wrap;
			
			Grid.SetColumn(lbl, 0);
			Grid.SetColumn(speech, 1);
			Grid.SetRow(lbl, 0);
			Grid.SetRow(speech, 0);
			
			Output.Children.Add(grid);
				
		}



called like:

Writer.character("Dave - ", "This is text speech.", Output);



Is This A Good Question/Topic? 0
  • +

Replies To: Dynamic Grids

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5890
  • View blog
  • Posts: 20,102
  • Joined: 05-May 12

Re: Dynamic Grids

Posted 25 September 2017 - 08:50 PM

Why are you using WPF controls as if they were WinForms controls? If you are using WPF, you should really use the MVVM design pattern so that you don't have to micro-manage each and every control that gets added in.

Anyway to answer your question, yes you need a column/row definition for ever column or row you want.

Why is Grid on lines 23-26 capitalized while the rest of your references to your new instance of grid lowercase? Perhaps Grid references a different control than grid.
Was This Post Helpful? 0
  • +
  • -

#3 JeremyBenson11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 10-January 14

Re: Dynamic Grids

Posted 26 September 2017 - 04:01 AM

Sorry, I didn't catch that spelling last night. Every tutorial I look for, for create controls always gives me code for forms method. It's annoying, lol.

MVVM doesn't work here, there's nothing to bind to controls. Not sure how you would bind dynamically added text. Isn't MVVM for binding classes to controls? This is a text game, text elements are added on a whim, of course, I'm not to bright with complex models, lol.

Edit:

Grid.SetColumn(lbl, 0); is a static class. I guess it might be conflicting with the Grid already in my view?

Is there an alternative setup that would allow me to achieve a name beside text. The name has to be separate, like a label, because I may attach click handlers to names, and certain labels in the game.

I tried a horizontal panel, or whatever it's called, but when the textblock reached the end of the stackpanel it dropped down under the name.

This post has been edited by JeremyBenson11: 26 September 2017 - 09:56 AM

Was This Post Helpful? 0
  • +
  • -

#4 JeremyBenson11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 10-January 14

Re: Dynamic Grids

Posted 26 September 2017 - 10:08 AM

I solved this my self. Sorry for the waste. If anyone is struggling with this type of thing, and wont lose anything by getting rid of the grid, you can use a dock panel. Dock both the label and the other control to the left.

That doesn't mean I'm not interested in an answer here though. Please address the issue if you can. I really feel let down by this, and as if this should have been possible. Please tell me if it's not.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5890
  • View blog
  • Posts: 20,102
  • Joined: 05-May 12

Re: Dynamic Grids

Posted 26 September 2017 - 10:09 AM

That's because you are likely looking at tutorials specifically covering how to add controls dynamically. You get what you asked for. Instead search for tutorials regarding data templating.
Was This Post Helpful? 0
  • +
  • -

#6 JeremyBenson11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 10-January 14

Re: Dynamic Grids

Posted 26 September 2017 - 10:40 AM

Yeah,

I spoke too soon. I didn't solve anything. What is with WPF and static objects to add controls. Do they expect everything to be perfectly laid out in xaml with no expectation of anyone adding anything to a program but text to a richtextbox? Seriously.

I've got the same problem, but I know the dock panel will look right from testing in xaml.

DockPanel panel = new DockPanel();
		
			Label lbl = new Label();
			lbl.Content = name;
			lbl.Padding = new System.Windows.Thickness(0);
			
			TextBlock speech = new TextBlock();
			speech.Padding = new System.Windows.Thickness(0);
			speech.Text = speechText;
			speech.TextWrapping = System.Windows.TextWrapping.Wrap;
			speech.Foreground = System.Windows.Media.Brushes.DarkGreen;
		
			DockPanel.SetDock(lbl, Dock.Left);
			DockPanel.SetDock(speech, Dock.Left);
			
			Output.Children.Add(panel);


This post has been edited by JeremyBenson11: 26 September 2017 - 10:41 AM

Was This Post Helpful? 0
  • +
  • -

#7 JeremyBenson11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 10-January 14

Re: Dynamic Grids

Posted 26 September 2017 - 10:47 AM

Datatemplate and binding has nothing to do with what I'm trying to accomplish. I do not have an already created layout that I want to bind data to. I have a text game layout, and it's curcial to just add stuff on the fly.. I don't have premade junk. I don't it's not important. There's got to be a way to attach elements like I'm trying to do.

This is my window

<Window x:Class="Dark_Awakening.Window1"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	Title="Dark_Awakening" Height="300" Width="300"
	>
	<Grid>
		<Grid.RowDefinitions>
			 <RowDefinition Height="*" />
			 <RowDefinition Height="*"  />
			 <RowDefinition Height="Auto"  />
		 </Grid.RowDefinitions>
		<Grid.ColumnDefinitions>
			<ColumnDefinition />
			<ColumnDefinition />
			<ColumnDefinition />
			<ColumnDefinition Width="*" />		
		</Grid.ColumnDefinitions>
		<ScrollViewer Grid.Row="0" Grid.RowSpan="2" Grid.Column="0" Grid.ColumnSpan="3">
			<StackPanel Name="Output">
			</StackPanel>
		</ScrollViewer>
		<Label Background="White" Grid.Row="0" Grid.RowSpan="2" Grid.Column="3" Grid.ColumnSpan="1" BorderThickness="0"></Label>
		<TextBox Name="Input" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3" FontSize="16"></TextBox>
		<Button Content="Enter" Grid.Row="3" Grid.Column="3" Height="28" FontSize="16"></Button>
	</Grid>
</Window>


It doesn't make sense to me to have classes and objects bound to this. The game runs on parsing. Or maybe I'm just not smart enough to grasp what is being suggested. Really I just need to know how to append the controls I want, lol.

Edit:

This is what I don't understand about the whole dynamic class and the creation of controls. Take these three simple lines for example:

DockPanel panel = new DockPanel();

DockPanel.SetDock(lbl, Dock.Left);
DockPanel.SetDock(speech, Dock.Left);




how does DockPanel relate to my instantiated panel? This is what every tutorial has suggested for creating dynamic controls, for both this control, and the grid, and neither has produced results. I also can't see how it would. The only time my instantiated panel was ever accessed was to add properties. It doesn't ever seem to be attached. IT makes no sense. There must be a way to tell the lbl, and speach textblock to attach to my instantiated element, and then attach that to my output stackpanel.

This post has been edited by JeremyBenson11: 26 September 2017 - 11:31 AM

Was This Post Helpful? 0
  • +
  • -

#8 JeremyBenson11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 10-January 14

Re: Dynamic Grids

Posted 26 September 2017 - 12:22 PM

Okay, I can see how data templates would be handy after reading a lot of webpages. They are all incomplete information. It shows a bit of xaml and a bit of code, and expects you to know everything.

I put this xaml inside my dockpanel:

<DataTemplate DataType="{x:Type Speech}">
	<DockPanel>
	       <Label DockPanel.Dock="Left">sdfsdfsdf</Label>
	       <TextBlock DockPanel.Dock="Left">This is text</TextBlock>
	</DockPanel>
</DataTemplate>



And get the error:

The type reference cannot find a public type named 'Speech'. Line 20 Position 46. (MC3066) - C:\Users\JeremyBenson11\Documents\SharpDevelop Projects\Dark Awakening\Dark Awakening\Window1.xaml:20,46



I created a class for speech, but it's not instantiated anywhere yet, I don't even know where to start. It would be nice if someone had a complete tutorial.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5890
  • View blog
  • Posts: 20,102
  • Joined: 05-May 12

Re: Dynamic Grids

Posted 26 September 2017 - 01:07 PM

This tutorial should be slightly easier to follow.

Now imagine what happens when instead of having that fixed hard coded list that is in that tutorial, you have an ObservableCollection where you dynamically add items. Presto! It's just like what you wanted to do above where in your text adventure things happen and you just need to add the line of dialog or narration to the collection.
Was This Post Helpful? 1
  • +
  • -

#10 JeremyBenson11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 10-January 14

Re: Dynamic Grids

Posted 26 September 2017 - 01:50 PM

Hey, sorry. I'm getting some results with the tutorial. I had to change code a lot. The problem with the listbox is each entry is selectable. Also, with labels added still be able to have event handlers attached to them?
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 5890
  • View blog
  • Posts: 20,102
  • Joined: 05-May 12

Re: Dynamic Grids

Posted 26 September 2017 - 02:11 PM

Change over to a stack panel instead of a listbox. You'll likely want the scrollbar provided by the listbox, though...
Was This Post Helpful? 0
  • +
  • -

#12 JeremyBenson11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 10-January 14

Re: Dynamic Grids

Posted 26 September 2017 - 02:50 PM

Hey, thanks. I'm right on the right track, but still having issues. I got the tutorial working perfectly in a test solution, but when copying the same method into my game I get nothing.

wpf
<Window x:Class="Dark_Awakening.Window1"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	Title="Dark_Awakening" Height="300" Width="300"
	>
	<Grid>
		<Grid.RowDefinitions>
			 <RowDefinition Height="*" />
			 <RowDefinition Height="*"  />
			 <RowDefinition Height="Auto"  />
		 </Grid.RowDefinitions>
		<Grid.ColumnDefinitions>
			<ColumnDefinition />
			<ColumnDefinition />
			<ColumnDefinition />
			<ColumnDefinition Width="*" />		
		</Grid.ColumnDefinitions>
		<ScrollViewer Name="Output" Grid.Row="0" Grid.RowSpan="2" Grid.Column="0" Grid.ColumnSpan="3">
			 <ItemsControl Margin="10" Name="SpeechBinding">
                        <ItemsControl.ItemTemplate>
                                <DataTemplate>
                                        <DockPanel>
                                                <Label Content="{Binding Name}" DockPanel.Dock="Left" Margin="6, 0, 0, 6"/>
                                                <TextBlock Text="{Binding Speech}" DockPanel.Dock="Left" FontWeight="Bold" TextWrapping="Wrap"  Margin="6, 4, 0, 6"/>
                                        </DockPanel>
                         </DataTemplate>
                </ItemsControl.ItemTemplate>
          </ItemsControl>
		</ScrollViewer>
		<Label Background="White" Grid.Row="0" Grid.RowSpan="2" Grid.Column="3" Grid.ColumnSpan="1" BorderThickness="0"></Label>
		<TextBox Name="Input" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3" FontSize="16"></TextBox>
		<Button Content="Enter" Grid.Row="3" Grid.Column="3" Height="28" FontSize="16"></Button>
	</Grid>
</Window>



window class
/*
 * Created by SharpDevelop.
 * User: JeremyBenson11
 * Date: 2017-09-20
 * Time: 6:17 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Text;
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.Collections.ObjectModel;

namespace Dark_Awakening
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>
	public partial class Window1 : Window
	{
		
		public ObservableCollection<Speech> talking = new ObservableCollection<Speech>();
		
		public Window1()
		{
			InitializeComponent();
			InitializeTalking();
			
			// create paths for game
			pathfinder PathFinder = new pathfinder();
			PathFinder.buildMode = "debug";
			PathFinder.buildRootPath();
			
			// create Flags object
			
			flags Flags = new flags();
			Flags.addFlag("game started", "false");
		
			// create database for game if not created
			DB gameDB = new DB(PathFinder);
			
			// writer
			
			
			if(Flags.returnFlag("game started") == "true")
			{
			
				Dictionary<string, string> parsed = new Dictionary<string, string>();
				
				if(parsed.ContainsKey("command search") && parsed["command search"] == "found")
				{
						
					// running commands here
					
				}
		
			}
			
			// end window function
		}
		
		public void InitializeTalking()
		{
			
			talking.Add(new Speech(){name = "text", speech = "text"});
			SpeechBinding.ItemsSource = talking;
			
		}
		
		// end window class
	}
	
	// end namepace
}


This post has been edited by JeremyBenson11: 26 September 2017 - 03:39 PM

Was This Post Helpful? 0
  • +
  • -

#13 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

Re: Dynamic Grids

Posted 26 September 2017 - 05:57 PM

You really need to create a DelegateCommand class and use ICommand to bind your controls, that way you can create your own events.

Delegate Commands
Was This Post Helpful? 0
  • +
  • -

#14 JeremyBenson11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 10-January 14

Re: Dynamic Grids

Posted 26 September 2017 - 07:00 PM

I can't figure these out. Inotifychange and delegates are over my head, and likely will be forever. This is not the first time I've failed to grasp Inotifychange. It's like difficult math, I can't get beyond basic highschool junk. Seriously, I don't know how anyone thinks these long hard brutal code paths are a better choice over simple to read OP with a bit of spaghetti code. lol. I can't help it, I'm just not smart enough for the other stuff. As soon as I get some money I'm going to find someone that will tell me how to just do it the way I was doing it. There should be a way to append items to stack panel.

I just want to go back to what I had.
<Window x:Class="Dark_Awakening.Window1"
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	Title="Dark_Awakening" Height="300" Width="300"
	>
	<Grid>
		<Grid.RowDefinitions>
			 <RowDefinition Height="*" />
			 <RowDefinition Height="*"  />
			 <RowDefinition Height="Auto"  />
		 </Grid.RowDefinitions>
		<Grid.ColumnDefinitions>
			<ColumnDefinition />
			<ColumnDefinition />
			<ColumnDefinition />
			<ColumnDefinition Width="*" />		
		</Grid.ColumnDefinitions>
		<ScrollViewer Grid.Row="0" Grid.RowSpan="2" Grid.Column="0" Grid.ColumnSpan="3">
			<StackPanel Name="Output">
			</StackPanel>
		</ScrollViewer>
		<Label Background="White" Grid.Row="0" Grid.RowSpan="2" Grid.Column="3" Grid.ColumnSpan="1" BorderThickness="0"></Label>
		<TextBox Name="Input" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3" FontSize="16"></TextBox>
		<Button Content="Enter" Grid.Row="3" Grid.Column="3" Height="28" FontSize="16"></Button>
	</Grid>
</Window>




C#

DockPanel panel = new DockPanel();
		
Label lbl = new Label();
lbl.Content = name;
lbl.Padding = new System.Windows.Thickness(0);
			
TextBlock speech = new TextBlock();
speech.Padding = new System.Windows.Thickness(0);
speech.Text = speechText;
speech.TextWrapping = System.Windows.TextWrapping.Wrap;
speech.Foreground = System.Windows.Media.Brushes.DarkGreen;
		
DockPanel.SetDock(lbl, Dock.Left);
DockPanel.SetDock(speech, Dock.Left);
			
Output.Children.Add(panel);




These are the problem lines I think:

DockPanel.SetDock(lbl, Dock.Left);
DockPanel.SetDock(speech, Dock.Left);



Nothing in the code above accesses the newly created dockpanel.

Lol, can we please leave the hard code to the geniuses? I'm not coding a damn hydron collider. Just a text game.

This post has been edited by JeremyBenson11: 26 September 2017 - 07:01 PM

Was This Post Helpful? 0
  • +
  • -

#15 JeremyBenson11  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 230
  • Joined: 10-January 14

Re: Dynamic Grids

Posted 26 September 2017 - 07:41 PM

Holy! I got it.

DockPanel panel = new DockPanel();
		
			Label lbl = new Label();
			lbl.Content = name;
			lbl.Padding = new System.Windows.Thickness(0);
						
			TextBlock speech = new TextBlock();
			speech.Padding = new System.Windows.Thickness(0);
			speech.Text = speechText;
			speech.TextWrapping = System.Windows.TextWrapping.Wrap;
			speech.Foreground = System.Windows.Media.Brushes.DarkGreen;
					
			panel.Children.Add(lbl);
			panel.Children.Add(speech);
			
			DockPanel.SetDock(lbl, Dock.Left);
			DockPanel.SetDock(speech, Dock.Left);
						
			Output.Children.Add(panel);



And I didn't have to create a rocket, with delegate commands from the LHC, to make my way to the corner store either!

Thanks for all your guyses efforts though. It does mean a lot, because usually I come to some kind of answer.

This post has been edited by JeremyBenson11: 26 September 2017 - 07:42 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2