WPF dynamic data display chart - label - c#

I am using Microsoft Visual Studio 2010, Includes Dynamic Data Display. I'm trying to adding on the chart a DYNAMIC label or textblock. I have been success to add a textblock on the grid but it move with the chart (I want it on the chart on specific point- not on the screen).
My adding textBlock :
TextBlock LenghtText = new TextBlock();
LenghtText.Margin = new Thickness(180, 50, 0, 0);
LenghtText.Text = lengthLine + "";
LenghtText.Width = 50;
myGrid.Children.Add(LenghtText);
// plotter.Children.Add(LenghtText); //--> If i try this line it error that LenghtText doesn't children of a IPlotterElement

Related

Stepping through controls in a wrappanel

I have a bunch of controls located in a wrappanel in a WPF app that are procedurally created. The first control is a label followed by a bunch of comboboxes and a checkbox. The user clicks a button and a new row of controls are added. This worked fine. Then I decided to make the label a bit more attractive by giving it a red circle as a background and the label was nested in a grid with the red circel behind the label that simply listed the row number. This worked fine. And I use to step through all the controls with this block:
foreach (Control item in WrapPanelItems.Children)
{
if (item.GetType() == typeof(CheckBox))
{
RowCounter++;
}
}
now suddenly this block of code fails with this error: 'Unable to cast object of type 'System.Windows.Controls.Grid' to type 'System.Windows.Controls.Control'.'
So I suspect the grid isnt a conventional item and the code fails. But how would I then iterate through the controls without the app crashing and still iterate through all the conventional controls ontop of it?
Here is the code for how I style and add the label.
Grid MyGrid= new Grid();
Ellipse myEllipse = new Ellipse();
SolidColorBrush mySolidColorBrush = new SolidColorBrush();
mySolidColorBrush.Color = Color.FromArgb(255, 107, 142, 35);
myEllipse.Fill = mySolidColorBrush;
myEllipse.Width = 20;
myEllipse.Height = 20;
MyGrid.Children.Add(myEllipse);
Label LabelCounter = new Label();
LabelCounter.Content = RowCount.ToString();
MyGrid.Children.Add(LabelCounter);
LabelCounter.VerticalAlignment = VerticalAlignment.Center;
LabelCounter.HorizontalAlignment = HorizontalAlignment.Center;
WrapPanelItems.Children.Add(MyGrid);
And also a second question. Suppose I want to change the text on the label... how would I get to the label if its nested in a grid? Can you just change the content directly when the FOR loop picks up the grid? Or should you then say all children of grid when the grid is identified in the FOR loop?
tx
Grid is not a System.Windows.Controls.Control so this line throws an exception:
foreach (Control item in WrapPanelItems.Children)
You can replace Control with UIElement to avoid this error.
According to MSDN:
public class Grid : System.Windows.Controls.Panel, System.Windows.Markup.IAddChild

Will Radiobutton created dynamically have fixed text size in Windows Form c#?

I have tried to create a radiobutton dynamically and add it to groupbox/form, but the whole text associated with the radiobutton is not getting displayed. When the radiobutton is added from the designer, the whole text is getting displayed. For dynamically adding radio button am I missing anything or are there any ways to do it?
Please find the sample code below:
public partial class Form1 : Form
{
private void SelectMicrophone_Load(object sender, EventArgs e)
{
System.Windows.Forms.RadioButton r1 = new System.Windows.Forms.RadioButton(); //created a radiobutton
r1.Name = "Microphone(RealTex";
r1.Text = "Microphone(RealTex";
r1.Location = new System.Drawing.Point(15, 15);
this.groupBox1.Controls.Add(r1);
When you set the text property in the designer, it adjust the radio button to the new size to cover the width of the text. By default I think the width is 90 and with the text above it is resized to a width of 124. So when you create the object at runtime, it is probably just keeping the width to 90. You can however just set r1.Width = 124 before adding it to your controls collection.
Keep in mind that you may not know the length each time so you could either set the width to the maximum size that you need or use the TextRender's .MeasureText method to get the size of the text and then just add 20 to that to cover the graphic of the radio button circle that also appears and set the result of the X property to your width before adding the radiobutton to the collection.
RadioButton r1 = new RadioButton();
r1.Text = "This is short text";
//Measure the Text property and get the width and add 20 to accomodate the circle
r1.Width = (TextRenderer.MeasureText(r1.Text, r1.Font)).Width + 20;
r1.Location = new Point(15, 15);
this.Controls.Add(r1);
//Just another RB with even longer text.
r1 = new RadioButton();
r1.Text = "This is even longer text that we want to show";
r1.Width = (TextRenderer.MeasureText(r1.Text, r1.Font)).Width + 20;
r1.Location = new Point(15, 35);
this.Controls.Add(r1);

Adding dynamic text boxes in windows store app 8.1 using xaml c#

TextBox x = new TextBox();
x.Height = 30;
x.Width = 200;
x.Name = "Title";
x.Text = item.Title;
x.TextWrapping = TextWrapping.Wrap;
x.FontSize = 60;
StackPanel s = new StackPanel();
s.Children.Add(x);
I have placed this code inside a function called private async void Getnotes();
and im calling this function from the constructor after this.InitializeComponent();
But when i run the app,text boxes are not getting added. what could be the problem?
You need to add the stackpanel to the window
window.AddChild(s);
You need to pass the window to your function.
by default wpf app has a non named grid.
name it "MyMainGrid".
and then you can add ether your stack panel.
MyMainGrid.Children.Add(s);
or directly add textbox to grid.
MyMainGrid.Children.Add(X);

WPF - DataGridColumn Width not Returning Expected Value

.NET4.0 : I'm building a DataGrid in a codebehind, so I'm not using any XAML. C# only. When the user right clicks anywhere in the column header, I want to show a context menu. Here's some code to give you an idea:
public void MakeAllColumns()
{
for (int i = 0; i < AllColumnDisplayNames.Length; i++)
{
// create a new column depending on the data to be displayed
DataGridTextColumn col = new DataGridTextColumn();
col.MaxWidth = 300;
// create a new Textblock for column's header and add it to the column
TextBlock headerText = new TextBlock() { Text = AllColumnDisplayNames[i] };
col.Header = headerText;
/// create a new context menu and add it to the header
ContextMenu menu = new ContextMenu();
headerText.ContextMenu = menu;
// build the context menu depending on the property
menu.Items.Add(new Button() { Content = "FOOBAR" });
// add the bindings to the data
col.Binding = new Binding(AllColumnBindings[i]);
AllColumns.Add(AllColumnDisplayNames[i], col);
}
}
The problem with this approach is that the user needs to click on the actual TextBox in order to activate the context menu, as opposed to anywhere on the header.
As I can't think of a way to make the TextBox fill the header width, all I can think to do is change the TextBox width property to bind to the column's width. The columns stretch to fit their content, and as such they have different widths. However, when I print all the columns ActualWidth properties to console, it says that they all have width 20, which is not what my GUI looks like. How can I get the column width that corresponds to how it looks in my GUI?
To you solve your problem must exchange o your body method by this body:
This code was tested:
for (int i = 0; i < AllColumnDisplayNames.Length; i++)
{
// create a new column depending on the data to be displayed
DataGridTextColumn col = new DataGridTextColumn();
col.MaxWidth = 300;
/// create a new context menu
ContextMenu menu = new ContextMenu();
// build the context menu depending on the property
menu.Items.Add(new Button() { Content = "FOOBAR" });
// create a new column's header and add it to the column
DataGridColumnHeader head = new DataGridColumnHeader() { Content = AllColumnBindings[i] };
head.ContextMenu = menu;//add context menu to DataGridColumnHeader
col.Header = head;
// add the bindings to the data
col.Binding = new Binding(AllColumnBindings[i]);
AllColumns.Add(AllColumnDisplayNames[i], col);
}
Instead of using TextBlock I used DataGridColumnHeader, which has the ContextMenu property, thus occupying all the space (height and width) of the Header.

My winform project has a panel, how do I draw button's and other toolbox items into the panel through code?

I know it isn't much, but this is all I got:
TextBox textBox = new TextBox();
textBox.Text = "sample text";
textBox.Width = 200;
textBox.Height = 50;
I have been programming c# for a while now, but Winforms are new to me. Google hasn't been of any help either.
You need to add the Textbox to the control's collection of your panel instance.
panel.Controls.Add(textbox);
YourPanel.Controls.Add(controlName);
And then: YourPanel.Location = new Point(Coordinates for X, Coordinates for Y); <- so you can move your control's position :P

Categories