I'm developing a windows phone app that represents a recipe book, so on the "add recipe" page I would like to add dinamically two textboxes representing ingredient name and quantity if the user decides to add a new ingredient.
My idea is to put the textboxes in a scrollviewer element, but I can't figure out how to add them dinamically from code and also how to retrieve data from the added textboxes: how can I name them so that I can retrieve data and exactly know which ingredient was added first, which second, etc?
Scrollviewer does not have a property to directly add elements such as stackpanel and grid which can directly add another element as child.
But Content property of Scrollviewer can be used to achieve this.
Add the two textboxes to the Grid(ContentPanel) firest and then set Grid as content to the scrollviewer
In code behind:
TextBox tb1 = new TextBox();
tb1.Name = "Name";
tb1.Text = "Ingredient Name";
TextBox tb2 = new TextBox();
tb2.Name = "Quantity";
tb2.Text = "Ingredient Quantity";
Grid gd = new Grid();
gd.Children.Add(tb1);
gd.Children.Add(tb2);
sc1.Content = gd;
In Window1.xaml
<ScrollViewer x:Name="sc1">
</ScrollViewer>
Related
I am creating and binding columns/elements into my datagrid.
I created a DataGridTemplateColumnand in that column i'm inserting a TextBox element.
DataGridTemplateColumn columnFeedbackSupplier = new DataGridTemplateColumn();
var dockPanel = new FrameworkElementFactory(typeof(DockPanel));
DataTemplate cellTemplate = new DataTemplate();
FrameworkElementFactory factoryText = new FrameworkElementFactory(typeof(TextBox));
Binding bindText = new Binding("Supplier");
bindText.Mode = BindingMode.TwoWay;
factoryText.SetValue(TextBox.TextProperty, bindText);
//This is not working
factoryText.SetValue(TextBox.VisibilityProperty, Visibility.Visible);
//
cellTemplate.VisualTree = factoryText;
dockPanel.AppendChild(factoryText);
cellTemplate.VisualTree = dockPanel;
Now when I add the following line, it allows me to edit my selected cell, but it also hides my TextBox on my datagrid until I double click on that cell then my element is visible again. Once that cell loses focus, my TextBox is hidden again.
columnFeedbackSupplier.CellEditingTemplate = cellTemplate;
I can use the following line to make my TextBox always visible, but I would not be able to save my edited values into my datagrid.
columnFeedbackSupplier.CellTemplate = cellTemplate;
Is there a way to make my TextBox always visible, while using CellEditingTemplate?
I've RadGrid where I want to add a column dynamically so I've done something like this in page load
GridTemplateColumn gtc = new GridTemplateColumn();
gtc.DataField = "chqNumber";
gtc.HeaderText = "Cheque Number";
gtc.UniqueName = "chqNumber";
RadGrid1.MasterTableView.Columns.Add(gtc);
It work fine, now I want to add textbox in this column as the user click "+add new record" on grid here is my code of itemCreated event
GridEditableItem gdit = (GridEditableItem)e.Item;
RadTextBox txtBox = new RadTextBox();
txtBox.ID = "someIDWhatEver";
gdit["chqNumber"].Controls.Add(txtBox);
but this textbox gets added to another new column what I mean is this textbox does not added to same chqNumber column you can view my attached image which will illustrate better
as you can see my dynamic column gets added at the last even after insert and cancel button and dynamic textbox doesn't gets in this column please avoid my english grammatical mistakes :P
This part works.
In my C#.NET WPF XAML, I have a static ComboBox and a static TextBox. The TextBox displays another column from the same DataTable (in the ComboBox's ItemSource). The column "rr_code" is the column for company name and the column "rr_addr" is the column for the address.
<ComboBox x:Name="CompanyComboBox1" IsEditable="True" IsTextSearchEnabled="True" IsSynchronizedWithCurrentItem="False"/>
<TextBox x:Name="StreetTextBox1" DataContext="{Binding SelectedItem, ElementName=CompanyComboBox1}" Text="{Binding rr_addr}" IsManipulationEnabled="True"\>
The ComboBox reads programmatically from a column in a DataTable:
CompanyComboBox1.ItemsSource = Rails.DefaultView; // Rails is a DataTable
CompanyComboBox1.DisplayMemberPath = "rr_code"; // column name for company name
This part doesn't work
The question is, I now have a "Add Company" button that creates a new form in a StackPanel, dynamically and with this exact functionality. The ComboBox works exactly as expected. Here's what I have so far:
ComboBox companyComboBox = new ComboBox();
companyComboBox.ItemsSource = Rails.DefaultView;
companyComboBox.IsEditable = true;
companyComboBox.IsTextSearchEnabled = true;
companyComboBox.DisplayMemberPath = "rr_code";
The problem is in the TextBox, which is not updating when I change the dynamic companyComboBox, so I'm sure it has to do with the binding.
TextBox streetTextBox = new TextBox();
streetTextBox.DataContext = companyComboBox;
Binding b = new Binding("rr_addr");
b.Mode = BindingMode.Default;
b.Source = companyComboBox.SelectedItem;
streetTextBox.SetBinding(ComboBox.SelectedItemProperty, b);
What is the correct way to set the binding for the TextBox streetTextBox?
The code-behind equivalent of this particular XAML + C# data binding in pure C# is:
ComboBox companyComboBox = new ComboBox();
companyComboBox.ItemsSource = Rails.DefaultView; // Rails being DataTable
companyComboBox.IsEditable = true;
companyComboBox.IsTextSearchEnabled = true;
companyComboBox.DisplayMemberPath = "rr_code";
Binding b = new Binding("SelectedItem.rr_addr"); // The selected item's 'rr_addr' column ...
b.Source = companyComboBox; // ... of the companyComboBox ...
TextBox streetTextBox = new TextBox();
streetTextBox.SetBinding(TextBox.TextProperty,b); // ... is bound to streetTextBox's Text property.
The error was in the last line. SetBinding needed to have a property of the target, not the source. In addition, the Binding declaration needed "SelectedItem." for some reason.
Why are you setting the TextBox DataContext ?
You can simply bind TextBox.Text property to ComboBox SelectedItem in your XAML
<TextBox Text="{Binding ElementName=CompanyComboBox1, Path=SelectedItem.rr_addr}"></TextBox>
I am using silverlight and coding in c# and i have to create a scrollbar on a list displayed.
Currently the list is displayed without scrollbar but it should contain one scrollbar as well.
My code to display the List is:
ListBox lines = new ListBox();
ScrollViewer scrollViewer = new ScrollViewer();
scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
lines.ItemsSource = param.Component.Attributes.Items;
Grid.SetColumn(lines, 1);
Grid.SetRow(lines, LoopCount);
childGrid.Children.Add(lines);
lines.SelectedIndex = 0;
lines.SelectedItem = param.Component.Attributes.Items;
lines.SelectionChanged += new SelectionChangedEventHandler(List_SelectionChanged);
lines.SelectedIndex = lines.Items.Count - 1;
"lines" variable contains my ListBox and i tried to achieve it throught scrollViewer but i dont know how to proceed further.
(Please note that i have to create selection changed event on selecting the items on GUI displayed on button click) so the code for scrollbar must not influence that procedure. And i have to code in c# only not in xaml. Thanks in advance for the help.
When you are puttig items Inside ScrollViewer You need to set the Content of ScrollViewer to ListBox,
change your code like below,
//Create ScrollViewer which is a child of Grid
var scroll = new ScrollViewer();
//add to the grid
childGrid.Children.Add(scroll);
//create lisbox and set it to the content of scrollviewer which is a Child of ScrollViewer
var listBox = new ListBox();
scroll.Content = listBox;
The Listview is Databound by Sql Table and am trying to add controls like textbox on Listview as below:
listviewBinding();
TextBox tb = new TextBox();
tb.BackColor = Color.Red;
tb.Text = "To Balance B/f";
tb.Width = listView1.Width;
listView1.Controls.Add(tb);
listView1.HeaderStyle = ColumnHeaderStyle.None;
The problem is that the top row is miss sizes and hence we can’t able to read it properly. What is the solution for it?.