I am working with asp.net page with telerik, in which i have a telerik radgrid, when click on the grid edit, need to fetch some data that matches present scenario and that data in the form of data table containing multiple rows,
Now based on the no of rows in data table i need to generate RadPanelItems programmatically, i achieved this by using the following code
for (int i = 0; i < dtCompletedCust.Rows.Count; i++)
{
RadPanelItem panelItem = new RadPanelItem();
panelItem.Text = dtCompletedCust.Rows[i]["CustName"].ToString();
pnlReviewedCust.Items.Add(panelItem);
}
It is successfully adding me the no of RadPanelItems,
Now my requirement is that I need to add content template to each of this newly added RadPanelItem and this content template contains multiple controls,
can any one help me or suggest some thing on this?
Create a class that will inherit ITemplate: https://msdn.microsoft.com/en-us/library/system.web.ui.itemplate(v=vs.110).aspx and instantiate it: https://msdn.microsoft.com/en-us/library/system.web.ui.itemplate.instantiatein(v=vs.110).aspx
Something like:
RadPanelItem panelItem = new RadPanelItem();
panelItem.Text = dtCompletedCust.Rows[i]["CustName"].ToString();
panelItem.ContentTemplate = myITemplateClass;
pnlReviewedCust.Items.Add(panelItem);
Or use its Controls collection:
RadPanelItem panelItem = new RadPanelItem();
panelItem.Text = dtCompletedCust.Rows[i]["CustName"].ToString();
panelItem.Controls.Add(new LiteralControl(DateTime.Now.ToString()));
pnlReviewedCust.Items.Add(panelItem);
Related
This question already has answers here:
Binding dynamically created control in code behind
(1 answer)
Dynamically Create Controls in MVVM
(1 answer)
Creating controls dynamically using xaml
(1 answer)
Closed 2 years ago.
I'm working with a MVVM appliaction in which I generate controls dinamically using a list with values taken from a database. The problem is that I don't know how to pass the values of the controls to the view model when the controls are generated dinamically.
I have created a class with the values that I want to pass to the view model like this:
class Bindeo {
int id;
string especifique;
bool padece;
public Bindeo(int id, string especifique, bool padece) {
id = this.id;
especifique = this.especifique;
padece = this.padece;
}
}
And i'm generating the values dinamically like this:
for (int x = 0; x < Math.Floor(cantidadElementosPorColumna) + residuo; x++) {
CAT_GINECOBSTETRICOS enfermedad = vm.EnfermedadesGinecobstetricas[x];
Grid grid = new Grid();
grid.ColumnDefinitions.Add(new ColumnDefinition());
grid.ColumnDefinitions.Add(new ColumnDefinition());
Label tituloEnfermedad = new Label {
Content = enfermedad.DESCRIPCION
};
Grid.SetColumn(tituloEnfermedad, 0);
grid.Children.Add(tituloEnfermedad);
Grid radios = new Grid();
radios.ColumnDefinitions.Add(new ColumnDefinition());
radios.ColumnDefinitions.Add(new ColumnDefinition());
RadioButton si = new RadioButton {
Content = "Si"
};
RadioButton no = new RadioButton {
Content = "No"
};
Grid.SetColumn(si, 0);
Grid.SetColumn(no, 1);
radios.Children.Add(si);
radios.Children.Add(no);
Grid.SetColumn(radios, 1);
grid.Children.Add(radios);
TextBox tb = new TextBox();
si.Command = vm.GenerarReporteCommand;
si.CommandParameter = new Bindeo((int) enfermedad.ID_CAT_GINECOBSTETRICOS, tb.Text, si.IsChecked.GetValueOrDefault());
EspecifiqueGinecobstetricos1.Children.Add(tb);
GinecobstetricosColumna1.Children.Add(grid);
}
What I'm doing here is that I want to bind to the si RadioButton a command in the view model, and send as parameter to the command the object Bindeo when a radio button is clicked. The values that I take for create the object Bindeo is the id of the object of the database that i'm referencing, the value of the textbox and the value of the siradio button, but when I click the radio button and the command is triggered, the object sended to the command is not taking the actulized values, instead is taken the values when the controls are created. How can I update the values to send them to the view model and not sending nulls?
What you're doing is not MVVM. In MVVM you never create controls directly, you instead assign data to properties and then write your XAML to respond to those changes. If at any point you need to call methods on GUI elements then you have a number of options e.g. behaviours.
In your specific case, you're trying to display a list of items, in which case you should start by using an ItemsControl. You then declare Data Templates in your XAML to specify how that data should be displayed, and you set the Panel Template to something other than the default StackPanel. Generally speaking, Grid isn't the best panel type to use for this, due to the need to also specify the row definitions, but there are certainly ways around that.
Here's a good tutorial to help get you started with all this, note the section titled "ItemsControl with data binding".
i want to add Items to my Listview, which i create dynamically using C#.
It looks kinda like this:
System.Windows.Controls.ListView test = new ListView();
test.Margin = new Thickness(28 + i, 23,485 - i, 23);
test.BorderBrush = new SolidColorBrush(Colors.Red);
test.BorderThickness = new Thickness(2);
test.VerticalContentAlignment = VerticalAlignment.Center;
Grid.SetColumn(test,1);
Grid.SetRow(test,1);
Now obviously the Listviews i create are completely empty.
So again my question: Is there any posibility to add items (say from an object) and display them in those Listviews i create here?
Ques : Is there any posibility to add items (say from an object) and display them in those Listviews i create here?
Let's say you get a list of string from an object. Like, objectMain.stringlist where objectMain is your object and stringlist is a property (List<string>) inside your object. Then, you can assign it like, test.ItemsSource = objectMain.stringlist
Using code behind is not recommended. Try using MVVM and create UIElements from the xaml.
I recommand you use MVVM patern to bind a list to ItemsSource in ListView and do that in xaml, but in code it is still available manually.
GridView gv = new GridView();
gv.Columns.Add(new GridViewColumn() { Header = "Something" });
test.View = gv;
test.Items.Add("Data1");
In my program i'm currently working on programmatically adding a variety of form objects in C#. For example i'm trying to create a certain group of panels, which contain the data I wish to show. When loading the form I have a file which contains all of the data I wish to load in, the loading itself works fine but when loading I need to create a variety of form labels, panels and images to display all of the necessary data and as such I need to make these panels and labels all with a seperate name, but programmatically.
for (int i=0; i<fileLength; i++)
{
Panel pnl = New Panel();
pnl.Name = "pnl1"+i;
//Set the other properties here
}
What i'm trying to do if use an iterator to append the current iteration to the end of the name. Am I going the right way about it? Or should I be doing a different method?
You cannot change variable/object name at runtime. If you want to write code against the object than you need to keep a reference to it. In your case you have changed the Name property of Panel but still you have to use it as pnl, not as pnl0 or pnl1.
The other way for doing this would be to use a Dictionary with key as the name as you assign and value as the object itself. This will help you in accessing your controls using its name that you have assigned to it.
Dictionary<string, Panel> panels = new Dictionary<string, Panel>();
for (i = 0; i <= 10; i++) {
Panel pnl = new Panel();
panels.Add("pnl" + i.ToString(), pnl);
}
//write code against panels
panels("pnl0").Width = 100;
For accessing within loop:
foreach (string pnlName in panels.Keys) {
panels(pnlName).Visible = true;
}
for (i = 0; i <= 10; i++) {
panels("pnl" + i).Visible = true;
}
I'm trying to add individualized and separate ContextMenus to each column header in my project, so that when a user right-clicks a header, a menu of checkboxes that relates to that header will appear that allows them to filter the data.
A couple of catches: the project that I'm working on needs to be developed for .NET 4.0, and as such I don't have access to the DataGridColumnHeader class that was introduced in .NET 4.5. Also, all of this needs to be done programatically, no XML allowed, as all of the column data is determined at runtime.
I found a similar Stack question in which this is done using XML, and I've sucessfully reproduced it in XML, but I'm new to WPF and haven't been able to reproduce it programatically.
I've pasted some C# code below where I think the setup should occur.
/// <summary>
/// Function that adds all of the columns for the default setup
/// </summary>
public void MakeAllColumns()
{
for (int i = 0; i < AllColumnDisplayNames.Length; i++)
{
DataGridTextColumn col = new DataGridTextColumn();
col.Header = AllColumnDisplayNames[i];
col.Binding = new Binding(AllColumnBindings[i]);
canGrid.Columns.Add(col);
// code for addding context menus will most likely go here
}
}
DataGridColumns are not FrameworkElements so they do not have a ContextMenu. Since you want to do this all in code behind specifying a data template for the column can be a pain (IMHO). You could try passing in a FrameworkElement as the Header object for the column and set the context menu on that framework element.
Example:
//...
DataGridTextColumn col = new DataGridTextColumn();
//...
TextBlock txt = new TextBlock() { Text = AllColumnDisplayNames[i] };
txt.ContextMenu = new ContextMenu(); // Build your context menu here.
col.Header = txt;
//...
I am developing a windows application using c# and I am using data bound grid of syncfusion controls. What I want is the list of of all columns with their properties like visible, read only and enable
You can try this if you are using GridDataControl.
var hiddencolumns = grid.VisibleColumns.Select(col => col.IsHidden == true);
If you are using GridDatsBound Grid , then you can get the column collection using the following code:
GridBoundColumnsCollection gc = this.gridDataBoundGrid1.Binder.InternalColumns
The other properties like Hidden, ReadOnly and Enabled can be accessed by using the following codes:
bool hidden, readalone , enabled;
for (int i = 0; i < gc.Count; i++)
{
hidden = gc[i].Hidden;
readalone = gc[i].ReadOnly;
enable = gc[i].StyleInfo.Enabled;
}
I hope this will resolve your issue.