Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Create dynamic grid structure using independency injection

$
0
0

This question is an extension of my previous question:

https://social.msdn.microsoft.com/Forums/vstudio/en-US/1d66106b-e24e-4014-a9ba-aae1d5d33ac1/how-to-dynamically-create-grid-structure?forum=wpf

The response for the question above was clearly understood. But I ran across an interesting material from Rachel that used dependency injection and I wonder how it could be applicable to my case.

https://rachel53461.wordpress.com/2011/09/17/wpf-grids-rowcolumn-count-properties/

I used Rachel's GridHelpers class and applied to my xaml static structure. It worked!

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
				x:Name='Test'
				local:GridHelpers.RowCount='2'
				local:GridHelpers.ColumnCount='5'
				local:GridHelpers.StarColumns='0,1,2,3,4'
				local:GridHelpers.StarRows='0,1'>

However, when I tried to bind it to a property to initialize it at run time, no error but no result

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
				x:Name='Test'
				local:GridHelpers.RowCount='{Binding rowCount, Mode=TwoWay}'
				local:GridHelpers.ColumnCount='{Binding colCount, Mode=TwoWay}'
				local:GridHelpers.StarColumns='{Binding starCol}'
				local:GridHelpers.StarRows='{Binding starRow}'>

My Code:

public sealed partial class MainPage : Page
	{
		private int _rowCount;
		private int _colCount;
		private string _starCol;
		private string _starRow;

		public string starRow
		{
			get { return _starRow; }
			set { _starRow = value; }
		}


		public string starCol
		{
			get { return _starCol; }
			set { _starCol = value; }
		}

		public int colCount
		{
			get { return _colCount; }
			set { _colCount = value;}
		}

		public int rowCount
		{
			get { return _rowCount; }
			set { _rowCount = value; }
		}

		public MainPage()
		{
			this.InitializeComponent();
			initiateGrid();
		}

		private void initiateGrid()
		{
			rowCount = 2;
			colCount = 5;
			starCol = "0,1,2,3,4";
			starRow = "0,1";
			for (int i = 0; i < 2; i++)
			{
				for (int j = 0; j < 5; j++)
				{

					StackPanel sp = new StackPanel();
					TextBlock tb = new TextBlock();
					sp.VerticalAlignment = VerticalAlignment.Stretch;
					sp.HorizontalAlignment = HorizontalAlignment.Stretch;

					tb.Text = string.Format("({0}, {1})", i, j);
					tb.FontSize = 20;
					sp.Children.Add(tb);
					Test.Children.Add(sp);
					Grid.SetColumn(sp, j);
					Grid.SetRow(sp, i);
				}
			}
		}

What was not done correctly? I know this is not a good way to build UI, but I am just curious why the code does not yield result it supposed to? There must be a piece that I have missed as I am very new to xaml, binding...

Thanks!


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>