Change UniformGrid child item during runtime - c#

I'm currently trying to populate a UniformGrid in WPF.
Some context: I'm trying to build a 2D Sidescroller (I know, WPF is not the best solution to that and I should use Unity or something else. I'd like to try it though). Now, for the map I was thinking about using a UniformGrid and using the children as tiles for my map.
So far, I'm adding empty children to the UniformGrid using this piece of code:
private void CreateMapTiles() {
for (int i = 0; i < 30; i++) {
for (int j = 0; j < 45; j++) {
this.MapGrid.Children.Add(new Image());
}
}
}
Where MapGrid is my UniformGrid.
So, in the end, I want to have 30x45 children.
Now I want to dynamically edit those children in CodeBehind, i.e. make those children an image. Or put another way, I want to update, let's say, the child at index 5 to display another image from my resources, than it did before.
I was thinking about using something like this:
this.MapGrid.Children[1] = new Image() { Source = ReturnSomeSource() };
But for obvious reasons, this doesn't work.
Is there any way i can assign a value to the children during runtime?
I've also played around with this.MapGrid.Children[1].SetCurrentValue() a bit, but neither did I really understand what I was doing, nor did it work in any way.
Does the possibility even exist, to change the UniformGrid children during runtime?
Would a normal Grid do a better job? I wanted to avoid that, because the UniformGrid children are arranged and sized automatically the way i need them without any more effort.

Rather than replacing Imagewith a new Image, change the source on the already existing image.
var tileImage = this.MapGrid.Children[1] as Image;
tileImage.Source = ReturnSomeSource();

Related

Cannot add a tooltip to appear when the mouse hovers to a unity EditorGUILayout.Popup()

I have a quite complex GUI in unity whit public override void OnInspectorGUI().
For all of my normal fieds I can add a tooltip with the header: Tooltip[("")] in the class itself.
However, I need a dynamic dropdown list that needs to be updated with the number of elements of a list.
I works great. Find the code of the custom editor script for the dropdown list:
string[] options = new string[movParamsListSize];
for (int i = 0; i < movParamsListSize; i++)
options[i] = i.ToString();
Rect r = EditorGUILayout.BeginHorizontal();
movStepToMoveTo.intValue = EditorGUILayout.Popup("movementStepToMoveTo",
movStepToMoveTo.intValue, options, EditorStyles.popup);
EditorGUILayout.EndHorizontal();
Find also a screenShot:
My problem is that I tried with all the overloaded functions of the EditorGUILayout.Popup() function (image below from the metadata) and cannot figure out how to add a tooltip when the mouse hovers to this control.
Any help is much apprecciated. Many thanks
Use the overload of EditorGUILayout.Popup that takes a GUIContent instead:
EditorGUILayout.Popup(new GUIContent("movementStepToMoveTo", "YOUR TOOLTIP HERE"), movStepToMoveTo.intValue, options);
Btw the EditorStyles.Popup is redundant since this is the default for a Popup ;)
and also HorizontalGroup is quite redundant here. You have only one control anyway.

How to add children to a specific to several grids

I have a WPF Project, Main window contains several grids.
After creating Textboxes and -blocks in a loop I add them to a grid using:
grid1.Children.Add(textbox1);
grid2.Children.Add(textbox2);
...
grid20.Children.Add(textbox20);
Is it somehow possible to replace this piece of code with a loop too?
If you have many of these grid/textboxes it might be wiser to wrap them in another control as Yaho Cho suggested in his comment..
I might be stating the obvious, but if you place the grids and textboxes in a list (i.e. create them in code and add them to the container in code) you can loop them.
List<TextBox> tl = new List<TextBox>(){ ... };
List<Grid> gl = new List<Grid>(){ ... }
for (i=0; i<20; i++)
{
gl[i].Children.Add(tl[i]);
container.Children.Add(gl[i]);
}

Draw bitmaps on window and update them 'x' times per second

Im new in Wpf C# programming and have maybe a stupid question.
I have a form and I need to create some controls with dynamic names.
(For Ex.: Grid: 'main' Controls: "str"+(int)i)
And I need to set Property Margin of this Controls OnTick.
So, Ik how to add this Controls, but have some problems in changing their properties.
Some code:
Image img = new Image();
img.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/img.png"));
img.Name = "str_" + i;
img.Margin = new Thickness(-10,-10,0,0);
img.Width = 1;
img.Height = 2;
main.Children.Add(img);
// ToDo Something like this:
main["str_"+i].Margin = new Thickness(x,y,0,0);
So, the question is: How to Edit property of already Created Dynamic control?
Update.
I found it rather stupid to create tons of controls except of refreshing drawings.
Now I have no idea what is the best way of drawing bitmaps on form.
For example:
I have List of locations of bitmaps.
I need to update the
bitmap's locations 'x' times per second.
Look at the next solution. Here is the code that references to the properties of an existing controls by it's (control's) name. It changes the control's set of properties according to the set of properties you support in view model (or code behind). If you will be interested I can make the adaptation to achieve your set of requirements. Here is the link
WPF user control, access dependency properties of component elements .
regards,

Looping through objects to get their properties in Windows Phone

I'm trying to develop a game in Windows Phone. I'm a beginner using c# and xaml. In the xaml I've objects with the same properties only Tag property is different. What I want to do is to loop througth object tags and change for another object (image) if I find a specific tag.
I've tried some code, like this:
foreach(Image tag in img) //but it says:
foreach statement cannot operate on variables of type 'Windows.Ui.Xaml.Controls.Image' because 'Windows.Ui.Xaml.Controls.Image' does not contain a public definition for 'GetEnumerator'...
Since as a beginner you won't likely use MVVM and just leave everything in the View, what you can do is just cycle through the LayoutRoot. If you have other Panel in the layoutroot that contain images, you want to do it recursively:
MainPage()
{
DoSomethingToImages(LayoutRoot);
}
DoSomethingToImages(Panel panel)
{
foreach(Image img in panel.Children.Where(x=> x is Image))
{
DoSomething(img);
}
var panels = panel.Children.Where(x=> x is Panel);
if (panels.Count > 0)
{
foreach(Panel p in panels)
{
DoSomethingToImages(p);
}
}
}
On the other hand, this is obviously bad practice, and you would normally bind your images to your viewmodel. The only exception might be heavily customised user controls to make things work that'd be otherwise extremely time-consuming to work around with MVVM. (Note: I think that MVVM is not always necessary for custom controls, since many times all you want is some custom graphical behaviour, like a button that takes an Image Background for it's pressed state as well, and that stuff belongs to the view, but you can usually solve that with a dependency property in the view and some XAML tweaking. Also, separating the ViewModel for a custom control makes it a bit harder to copy it to another project if you put it inside your own project - you have to find the viewmodel as well! :) ).
Like others have said, you'll need a collection to loop through.
Personally, I like dictionaries.
Here's an example I think you might be able to modify for your own purposes.
This will enable you to "loop though object tags", however as HighCore mentioned, it's probably not "the right way" to do whatever you're ultimately trying to accomplish.
Image img1 = new Image();
Image img2 = new Image();
Image img3 = new Image();
img1.Tag = "tag1";
img2.Tag = "tag2";
img3.Tag = "tag3";
Dictionary<string, Image> ImgDictionary = new Dictionary<string, Image>();
ImgDictionary.Add(img1.Tag.ToString(), img1);
ImgDictionary.Add(img2.Tag.ToString(), img2);
ImgDictionary.Add(img3.Tag.ToString(), img3);
foreach (KeyValuePair<string, Image> i in ImgDictionary)
{
// do stuff with i.Value or i.Key
}
string tmp_TagName = "tag1";
if (ImgDictionary.ContainsKey(tmp_TagName))
{
Image ReturnImage;
ImgDictionary.TryGetValue(tmp_TagName, out ReturnImage);
// do something with your ReturnImage...
}

Problem with layout of control when dynamically creating winform and controls for the Form

I get this response from a web service call. Something like this
<Response>
<Control1 type = "DropdownList" value= "USA,UK,Sweden,UAE"/>
<Control2 type = "Textbox" value= "Contries"/>
<Control3 type = "Button" value= "None">
</Response>
Based on this I de-serialize it into List<Controls>.
Now I need to be able to dynamically create a winform based on these controls. My problem is the layout. I want to be able to create them nicely separated (If possible vertically aligned) in batches of lets say 5.So If I need 15 controls I have 3 columns and 5 rows.
What would be best way to achieve this? I know that I can use the inbuilt positioning properties like top, width etc., but maybe someone out there has done something similar in a better way.
I think you should use TableLayoutPanel. Also you can read best practices to use this control.
One benefit of using TableLayoutPanel from above article:
Layouts that will be modified or generated dynamically at run time, such as data entry forms that have user-customizable fields added or subtracted based on preferences.
So basically I am gonna do something like this(might be useful for someone else)
Form op = new Form();
FlowLayoutPanel panel = new FlowLayoutPanel();
op.Controls.Add(panel);
for (int i = 0; i < 10; i++)
{
Button b = new Button();
b.Text = "Button" + i.ToString();
panel.Controls.Add(b);
}

Categories

Resources