TreeView and HierarchicalDataTemplate in code - treeview

I am trying to set up a TreeView with code only from an unnested list of objects with various properties. I set up SortDescriptions and GroupDescriptions, but I cannot figure out how to configure the HierarchicalDataTemplate to the CollectionViewSource groups.
Can someone give me a code-only example?
I've got:
ObservableCollection<AnimalObject> myAnimals = new ObservableCollection<AnimalObject>();
myAnimals.Add(new AnimalObject("mammal","cat"));
myAnimals.Add(new AnimalObject("mammal","dog"));
myAnimals.Add(new AnimalObject("bird","canary"));
myAnimals.Add(new AnimalObject("bird","eagle"));
myAnimals.Add(new AnimalObject("reptile","snake"));
myAnimals.Add(new AnimalObject("reptile","lizard"));
myAnimals.Add(new AnimalObject("reptile","dragon"));
ListCollectionView view = CollectionViewSource.GetDefaultView(myAnimals) as ListCollectionView;
view.GroupDescriptions.Add(new PropertyGroupDescription("Category"));
view.SortDescriptions.Add(new SortDescription("Category", ListSortDirection.Ascending));
view.SortDescriptions.Add(new SortDescription("AnimalName", ListSortDirection.Descending));
...and pretty much don't know what to do at that point. I wanted the TreeView to display categories that would expand to show the animals.
Thanks.

Related

Is it possible to change labels of Kendo Grid Editor?

I am using kendo grid and its build-in functionality for creating and updating items.
I'm looking for a way to change Editor labels (title and buttons Update/Cancel).
I found an answer here (Change Button text in Kendo Ui Grid Popup Window) where OnaBei explains how to change title.
However, I still cannot figure out the way to change button names based on whether item is being added or being edited. The same with title, is it a way to change it based on "create"/"update" state?
I assume that it can be done with javascript, but it will probably be a hack and dirty solution.
This can be done in the edit event of the grid. The model event argument has a isNew method which will return true in "create" state. Here is some sample code:
edit: function(e) {
var title = "Edit mode";
if (e.model.isNew()) {
title = "Insert mode";
}
var wnd = e.container.data("kendoWindow");
wnd.title(title);
}
And a live demo: http://jsbin.com/USUpAZUT/1/edit

Populating a grid via autocomplete and posting the results

I have a view where I create a new company.
The company has a number of trades, or which 1 is a primary trade.
So when I enter the trades for that company, I select a trade via autocomplete, and this trade is added to a grid of trades underneath the autocomplete textbox. The grid contains the tradeId as a hidden field, the trade, and a radio button to indicate whether the trade is a primary trade and a remove button.
This is part of a form that contains other company details such as address.
Now I am wondering if I can use knockout and (maybe) jsrender to populate the grid without posting to the server?
When I have filled in the grid AND the other company details, I then want to submit the data to the controller post method.
Normally I use the Html helpers to post values to the controller, but I don't see how I can do that using knockout.
Yes you can use Knockout for this. If you have not checked the tutorials out yet then try this Knockout List and Collections tutorial. This should point you in the right direction. What you'll need to do is create a Trade object with observable properties and in a separate knockout view model create an observableArray to store trade objects. For information on posting to the server there are other tutorials in the same location.
function Trade(item) {
var self = this;
self.tradeId = ko.observable(item.tradeId);
self.tradeName = ko.observable(item.tradeName);
self.isPrimary = ko.observable(item.isPrimary);
}
function TradesViewModel() {
var self = this;
// Editable data
self.trades = ko.observableArray([]);
self.removeTrade = function(trade) { self.trades.remove(trades) }
self.save = function() {
$.post("/controller/action", self.trades);
}
}
ko.applyBindings(new TradesViewModel());

LINQ Combobox Databinding behave weird

I'm having a WinForm on witch I've a DataGridView that display a list of sites. bellow that grid i've a TextBox and a Combobox, using DataBinding the textbox show the current value "Code" grid column and the combobox show the province. The combobox is filled with a list for province
The weird thing is this:
When I change the combobox selected province for another one, at the time when the focus leave the combobox, the original value of the of the combobox return. I can't find a way for the combobox to keep the changed value.
what am I doing wrong?
Here my the code of my workbench project;
private void Form1_Load(object sender, EventArgs e)
{
context = new GMR_DEVEntities();
lSite = from t in context.tblSites where t.Actif == true select t;
this.dataGridView1.DataSource = lSite;
this.dataGridView1.AutoGenerateColumns = true;
Guid ProvinceId = Guid.Parse("00000000-0001-0000-0008-000000000001");
IQueryable<tblDomVal> provinces = from prov in context.tblDomVals where prov.pTypeDomValID == ProvinceId select prov;
comboBox1.DataSource = provinces;
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "DescrFr";
textBox1.DataBindings.Add(new Binding("Text", lSite, "Code"));
comboBox1.DataBindings.Add(new Binding("SelectedValue", lSite, "pProvinceID", false, DataSourceUpdateMode.OnPropertyChanged));
}
Any help would be welcome :)
Hugo
Try creating a new binding context for you combo
BindingContext oBC = new BindingContext();
comboBox1.BindingContext = oBC;
comboBox1.DataBindings.Add(new Binding("SelectedValue", lSite, "pProvinceID", false, DataSourceUpdateMode.OnPropertyChanged));
While searching for a solution, I've discover that the problem have something to do with the Datatype (All my tables IDs are GUID), I've change the Binding to bind with a varchar field instead of the guid, and thing went good. So the problem have something to do with GUID. But I'll change my approach, and work with manual bind

Load asp.net control via AJAX

This is my scenario:
many usercontrols, each of them contains many gridviews.
I want to (re)load a single gridview via an ajax call to get rid of postbacks and to improve performances and user experience.
I don't want to rewrite tons of code so I just want to render that single gridview of that single control. <br/>
It means that if the gridview shows 4 fields and it has a bound server method it must be executed picking the rules from the .ascx.cs and the .ascx.
I cannot create a new instance of a gridview cause i need it to get created based on the .ascx rules.
I used this piece of code to create the grid and put it into the textwriter.
System.Web.UI.WebControls.GridView grid = new System.Web.UI.WebControls.GridView();
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
{
using (HtmlTextWriter textWriter = new HtmlTextWriter(sw))
{
grid.DataSource = ds;
grid.DataBind();
grid.RenderControl(textWriter);
}
}
And it comes out like a normal grid as expected. But I don't need this; I need to generate a grid that is exactly the same as the one defined in the .ascx.
You should use an <asp:UpdatePanel>.

System.Web.MVC.SelectList how to set selected item upon creation

trying to have an item in a DropDownList selected by default from within my viewmodel.
I am creating the select list in my view model using teh following overload..
public SelectList ProgramsSelectList()
{
var ddlist = new SelectList(DiversionPrograms, "DiversionProgramID", "CrimeName", new {value = "1"});
return ddlist;
}
value = 1 is just hard coded for now. I am not sure the Html.DropDownList() respects this selected item.
Razor View:
#Html.DropDownList("programSelect", Model.ProgramsSelectList(), "Choose a Program to Manage")
I must be missing something here...
Figured it out...
var ddlist = new SelectList(DiversionPrograms, "DiversionProgramID", "CrimeName", DiversionProgram.DiversionProgramID);

Resources