I am new to windows phone. I am trying to make pivot of questions . I want to add a text block and 2 radio buttons on each pivot item . I managed to add the textbook but didn't know how to add radio buttons.
var count = i + 1;
var textblok = new TextBlock { Text = o["questions"][i]["question"].ToString(), FontSize = 20,Width=450};
textblok.TextWrapping = TextWrapping.Wrap;
quizPivot.Items.Add(new PivotItem { Name="question"+count, Header = "Question " + count, Content = textblok,});
after adding container
for (var i = 0; i < Globals.quizcount; i++)
{
var count = i + 1;
var stackpanel = new StackPanel();
var textblok = new TextBlock { Text = o["questions"][i]["question"].ToString(), FontSize = 20,Width=450};
textblok.TextWrapping = TextWrapping.Wrap;
stackpanel.Children.Add(textblok);
var radio = new RadioButton { Name = "useransYes", Content = "Yes" };
stackpanel.Children.Add(radio);
var radio1 = new RadioButton { Name = "useransNo", Content = "No" };
stackpanel.Children.Add(radio1);
//, HorizontalAlignment = "Left", Margin = "66,317,0,0", VerticalAlignment = "Top
quizPivot.Items.Add(new PivotItem { Name = "question" + count, Header = "Question " + count, Content = stackpanel });
quesId.Text = o["questions"][i]["_id"].ToString();
}
2nd i want to know how to get all the pivot items i mean the contents in it.
Thanks
You need to use a container control to add multiple UI controls to single PivotItem. For example using StackPanel as container :
//create the container
var stackpanel = new StackPanel();
//create textblock
var textblok = new TextBlock { Text = o["questions"][i]["question"].ToString(), FontSize = 20,Width=450};
textblok.TextWrapping = TextWrapping.Wrap;
//add to container
stackpanel.Children.Add(textblok);
//create radiobutton
var radiobutton = new RadioButton{Content = "Radio Button content"}
//add to container
stackpanel.Children.Add(radiobutton);
//add the container as content of pivot item
quizPivot.Items.Add(new PivotItem { Name="question"+count, Header = "Question " + count, Content = stackpanel,});
Anyway, there is another way around to accomplish this with much cleaner approach. Avoid creating UI controls from code by using data-binding and templating pivot item.
Related
I'm using WinForms in VS15 with C#.
I'm dynamically adding TextBoxs and Labels to my Form based upon a user selected value in a ComboBox (essentially this looks up a value in a data collection which tells my UI what controls it needs).
When I attempt to generate the controls, the Labels appear and layout just fine, however, the TextBoxs are remarkable in there absence.
I've tried fidelling with the MaximumSize and MinimumSize properties to see if they could be messing with something but it doesn't seem to be making any difference.
The code I use for doing this is below (I know the use of the List<Pair<Label,TextBox>> is pretty unecessary but I find it helps readability):
private void GenerateControls(string formType)
{
string[] formParameters = engine.GetFormParameters(formType);
if (formParameters == null) return;
SplitterPanel panel = splitContainer.Panel1;
panel.Controls.Clear();
List<Pair<Label, TextBox>> controlPairs = new List<Pair<Label, TextBox>>();
int tabIndex = 0;
Point labelPoint = panel.Location + new Size(20, 20);
Size initialOffset = new Size(0, 30);
Size horizontalOffset = new Size(40, 0);
Size tBoxSize = new Size(40,20);
foreach (string parameter in formParameters)
{
Label label = new Label
{
Text = parameter,
Tag = "Parameter Label",
Name = $"lbl{parameter}",
Location = (labelPoint += initialOffset)
};
TextBox textBox = new TextBox
{
AcceptsTab = true,
TabIndex = tabIndex++,
Text = "",
Tag = parameter,
Name = $"txt{parameter}",
MaximumSize = tBoxSize,
MinimumSize = tBoxSize,
Size = tBoxSize,
Location = labelPoint + horizontalOffset
};
controlPairs.Add(new Pair<Label, TextBox>(label, textBox));
}
foreach (Pair<Label, TextBox> pair in controlPairs)
{
panel.Controls.Add(pair.First);
panel.Controls.Add(pair.Second);
}
}
I don't believe that my use of Point + Size is the issue as the Point class overrides the + operator like so:
Unfortunately for me the issue appears to be simply that the dX was not a big enough value to prevent the text boxes from being hidden under the labels, I forgot that labels don't have transparent backgrounds.
While I was at it: I've removed the redundant List<Pair<<>>; added support for dynamically adjusting TextBox location based on Label size; and split it out into two separate loops, so my code now looks as below and works just fine:
private void GenerateControls(string formType)
{
string[] formParameters = engine.GetFormParameters(formType);
if (formParameters == null) return;
SplitterPanel panel = splitContainer.Panel1;
panel.Controls.Clear();
int tabIndex = 0;
Point labelPoint = panel.Location + new Size(20, 20);
Size verticalOffset = new Size(0, 30);
Size tBoxSize = new Size(200,20);
int maxLabelLength = 0;
foreach (string parameter in formParameters)
{
Label label = new Label
{
Text = parameter,
Tag = "Parameter Label",
Name = $"lbl{parameter}",
Location = (labelPoint += verticalOffset),
AutoSize = true
};
panel.Controls.Add(label);
if (label.Size.Width > maxLabelLength)
{
maxLabelLength = label.Size.Width;
}
}
Size horizontalOffset = new Size(maxLabelLength + 30, 0);
labelPoint = panel.Location + new Size(20, 20) + horizontalOffset;
foreach (string parameter in formParameters)
{
TextBox textBox = new TextBox
{
AcceptsTab = true,
TabIndex = tabIndex++,
Text = "",
Tag = parameter,
Name = $"txt{parameter}",
MaximumSize = tBoxSize,
MinimumSize = tBoxSize,
Size = tBoxSize,
Location = labelPoint += verticalOffset
};
panel.Controls.Add(textBox);
}
}
Thanks everyone who helped!
I'm using this accordion control in WPF application : ayoutToolkit:Accordion.
I need to make dynamic content from database.
I tried like this:
private void RebuildView(Accordion accordion)
{
var scrollView = new ScrollViewer { VerticalScrollBarVisibility = ScrollBarVisibility.Auto};
var grid = new Grid
{
ShowGridLines = true,
ColumnDefinitions =
{
new ColumnDefinition(),
new ColumnDefinition(),
}
};
int i = 0;
foreach (AttributeModel item in ViewModel.Attributes)
{
RowDefinition row = new RowDefinition { Height = new GridLength(60) };
grid.RowDefinitions.Add(row);
var label = new Label { Content = item.label, Foreground = Brushes.Black, FontSize = 20 };
Grid.SetRow(label, i);
Grid.SetColumn(label,0);
grid.Children.Add(label);
i++;
}
scrollView.Content = grid;
accordion.ContentTemplate = new DataTemplate(scrollView);
}
but I'm not sure how to make it.
I am working on silverlight using c#. I have to display the combo items in a scrollbar.
My attempt to do this is:
TextBlock txtblkName = generateTextBlock();
ComboBox cb = new ComboBox();
ScrollViewer scrollViewer = new ScrollViewer();
cb.Width = 45;
cb.Height = 20;
foreach (String item in param.Component.Attributes.Items)
cb.ItemsSource = param.Component.Attributes.Items;
scrollViewer.Content = cb;
scrollViewer.HorizontalAlignment = HorizontalAlignment.Center;
scrollViewer.VerticalAlignment = VerticalAlignment.Center;
scrollViewer.ScrollToVerticalOffset(3);
cb.SelectionChanged += (o, e) =>
{
txtblkName.Text = cb.SelectedValue.ToString() + " " + param.Unit;
};
cb.SelectedIndex = param.Component.Attributes.Selected != -1 ? param.Component.Attributes.Selected : 0;
Grid.SetColumn(scrollViewer, 1);
childGrid.Children.Add(scrollViewer);
which results in scroll over combo box .Like this:
Not on it's item displayed by scrollbar.
Could some one please help me to create scrollbar only on items displayed not on all combo box?
You dont need ScrollViewer here, If you need Scrollbar for your comboboxItems set this property MaxDropDownHeight to some value
ComboBox cb = new ComboBox();
List<string> items = new List<string>();
items.Add("1");
items.Add("2");
items.Add("3");
items.Add("5");
items.Add("7");
items.Add("8");
cb.ItemsSource = items;
cb.MaxDropDownHeight = 20;
childGrid.Children.Add(cb);
Well, I want to display two text boxes next to next in customMessageBox. So i have coded for two Text Boxes. like below. I named soora and ayath for it. But in customMessageBox, i cannot call two text boxes in the same time. It shows error. How to display two text boxes next to next in customMessageBox. I only the error and it is form Content = soora + ayath
My C# CODE;
TextBox soora = new TextBox();
soora.Height = 72;
soora.Width = 150;
soora.MaxLength = 3;
TextBox ayath = new TextBox();
ayath.Height = 72;
ayath.Width = 150;
ayath.MaxLength = 3;
CustomMessageBox messageBox = new CustomMessageBox()
{
Title = "GO TO",
Content = soora + ayath,
RightButtonContent = "Go !",
};
use a container control to hold both textboxes
TextBox soora = new TextBox();
soora.Height = 72;
soora.Width = 150;
soora.MaxLength = 3;
TextBox ayath = new TextBox();
ayath.Height = 72;
ayath.Width = 150;
ayath.MaxLength = 3;
StackPanel container = new StackPanel{
Orientation = System.Windows.Controls.Orientation.Horizontal
};
container.Children.Add(soora);
container.Children.Add(ayath);
CustomMessageBox messageBox = new CustomMessageBox()
{
Title = "GO TO",
Content = container,
RightButtonContent = "Go !",
};
if you want to display text then
Content = soora.Text + ayath.Text,
I am creating the DataGridTemplateColumn dynamically.
var binding = new Binding
{
Path = new PropertyPath("MyProperty"),
UpdateSourceTrigger = UpdateSourceTrigger.LostFocus
};
var converterParameter = new List<object> { header, rows, myGrid };
binding.Converter = new MyConverter();
binding.ConverterParameter = converterParameter;
var textBoxValue = new FrameworkElementFactory(typeof(TextBox));
totalUnitsValue.SetBinding(TextBox.TextProperty, binding);
totalUnitsValue.SetValue(TextBox.HorizontalContentAlignmentProperty, HorizontalAlignment.Right);
totalUnitsValue.SetValue(TextBox.WidthProperty, 40.0);
totalUnitsValue.SetValue(TextBox.MarginProperty, new Thickness(4, 0, 10, 0));
var factoryElement = new FrameworkElementFactory(typeof(StackPanel));
factoryElement.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
factoryElement.AppendChild(textBoxValue );
var column = new DataGridTemplateColumn
{
Header = header,
CellTemplate = new DataTemplate { VisualTree = factoryElement }
};
myGrid.Columns.Add(column);
This works fine for few columns. But if i create 10 or more columns (80 -90) textBoxes then the last created TextBoxes do not allow me to change the value or do not allow me to put focus on the TextBox. It becomes like TextBlock.
EDIT:
IF I REMOVE THE STACKPANEL, THEN THERE IS NO ISSUE WITH THE TEXTBOX BUT I NEED TO SHOW MORE THAN ONE ELEMENT, SO I NEED TO HAVE SOME SORT OF CONTAINER.ANY HELP ON THAT.
Please guide what could be tghe