|
The TreeView uses TreeViewItem(s). You need to add TreeViewItems to the TreeView.Items collection and you can add sub-items to a TreeViewItem.Items collection. To do what you want to do, you would need to set the Header to the Item and then set the template to properly bind.
One word of warning though. You are going to hate yourself in a few months for adding items by hand.
Use data bound collections and hierarchial templates instead.
|
|
|
|
|
The reason I am adding TreeViewItem manually is because I need the user to be able to add new child nodes to existing nodes through the UI.
I tried this
TreeViewItem tvi = new TreeViewItem();
tvi.Header = new Item("New Node");
TreeView.Items.Add(tvi);
And this header
<DataTemplate x:Key="TreeViewHeaderReadTemplate">
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Text}"></TextBox>
</StackPanel>
</DataTemplate>
But what I get is a textblock with the caption = TestApplication.Item ( ie. Full class name of item )
What I am looking for is a textbox with the editable text "New Node".
|
|
|
|
|
If you use data binding, users can still add / delete new nodes. You just add / remove them from the data bound collection and the UI will auto-magically update.
I believe your other issue is that you are not specifying TargetType on the DataTemplate. If you are getting TestApplication.Item, its because the TreeViewItem is not picking up the template.
Also, your Item class is not implementing INotifyPropertyChanged, so none of your setters will work properly.
|
|
|
|
|
I think you're going about this the wrong way... Yes, you can work directly with the TreeView, but that's not how it's supposed to be used. The idea is to do everything via data-binding, so you never have to touch the TreeView or TreeViewItems at all.
Here's how I would go about this:
1) Add a "Children" property to your Item (Can call it whatever you want), typed as, say, an ObservableCollection<Item> . You may also want to create boolean properties such as "IsExpanded" and "IsSelected", which can be bound in the TreeView.ItemContainerStyle.
2) As another poster suggested, implement INotifyPropertyChanged on your Item class, and make sure you're raising the PropertyChanged event when the Text property changes.
3) Use a HierarchicalDataTemplate in your XAML... This is basically a DataTemplate that also has an ItemsSource property to include children... Something simple, like:
<HierarchicalDataTemplate DataType="{x:Type local:Item}" ItemsSource="{Binding Children}">
<TextBox Text="{Binding Text}"/>
</HierarchicalDataTemplate>
(You can expand on it later)
4) Set the ItemsSource property of your TreeView to the root node of your tree. It'll automatically generate the rest of the nodes as you modify your Item and its children.
Now, when you want to add another node under your root, just pop one into the Children collection, and the TreeView will detect this and update itself. Basically, you should never have to touch the TreeViewItems directly.
|
|
|
|
|
My datagrid is populated with data.
One of the columns in a checkbox
After population, I check or uncheck a nmber of rows in the datagrid.
Say in a click event i.e. update_click(), how can I loop through th edatagrid rows to see which rows are updated with the checks or unchecks?
In the update button click event I am trying to use this code:
List<ClassName> source = (List<ClassName>)grid.ItemsSource;
foreach (ClassName monitor in source)
{
}
But on th efirst line I get the error: unable to case object of type ...observablecollection to type ...list
Any thoughts please?
Thank you
Thanks
modified on Thursday, January 6, 2011 5:32 AM
|
|
|
|
|
It's already told you what the problem is. The backer for grid.ItemsSource is an ObservableCollection, and you're trying to cast it into a List. ObservableCollection does not derive from List, so you can't cast to it. What you could do, however, is cast it to IList instead, so you'd get:
IList<ClassName> source = (IList<ClassName>)grid.ItemsSource
|
|
|
|
|
Thanks for replying.
I remembered that once I read somewhere that I can use the observablecollection so I replaced the list with that.
And it works.
Thank you
|
|
|
|
|
Hi,
I have an object that is a collection of orders objects, each order object has a collection of OrderItems, each OrderItem has a collection of ItemDetail objects.
Im displaying all this data in 3 list boxes and the current selected value in any list box is displayed in a textbox.
I set the DataContext of my grid to the object. - All data is displayed as expected in WPF but when I try something similar in Silverlight it doesnt show any data beyond the parent...
Here is the xaml:
<ListBox Grid.Column="0" Name="lstOrders" DisplayMemberPath="OrderName" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" />
<ListBox Grid.Column="1" Name="lstOrderItems" DisplayMemberPath="Product" ItemsSource="{Binding Path=OrderItems}" IsSynchronizedWithCurrentItem="True" />
<ListBox Grid.Column="2" Name="lstItemDetails" DisplayMemberPath="Details" ItemsSource="{Binding Path=OrderItems/ItemDetails}" IsSynchronizedWithCurrentItem="True" />
<TextBox Grid.Column="0" Grid.Row="1" x:Name="txtOrderName" Text="{Binding Path=OrderName}" />
<TextBox Grid.Column="1" Grid.Row="1" x:Name="txtProduct" Text="{Binding Path=OrderItems/Product}" />
<TextBox Grid.Column="2" Grid.Row="1" x:Name="txtDetail" Text="{Binding Path=OrderItems/ItemDetails/Details}" />
Any ideas what the Bindings should be for this to work in Silverlight?
|
|
|
|
|
Chazzysb wrote: OrderItems/ItemDetails/Details
Try Path=OrderItems.ItemDetails.Details
Use . instead of /.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
|
|
|
|
|
Hi,
I tried setting the datacontext to the control, the grid, from another class property using the mvvm model and countless variations of . & / still this refused to work in Silverlight.
If anyone has a working example id be interested in seeing it!
I managed to get it working however by doing it this way:
http://msdn.microsoft.com/en-us/library/cc645060(v=VS.95).aspx[^]
Thanks,
Chas
|
|
|
|
|
I want to create a theater management system in which I have to dynamically add the seats as present in the table of database. Imagine a theater where there are seats from Row A to J and seat number 1 to 10. I want to make sure that if the seat is booked, I have to show it Red, if its vacant, I have to show it in green and if its disabled, it must be grayed out. I also want to create the table for such layout in Silverlight... Any suggestions?
Always Keep Smiling.
Yours Pankaj Nikam
|
|
|
|
|
Homework - we've had this question at least twice before, and we don't do homework for people. It's not that we feel we're too good to do it, it's that you won't learn anything if we do it for you. Part of being a programmer is being able to anaylze the problem, establish requirements, create the design, and implement the solution by developing the code.
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
|
Hi,
I have to do a validation to check the duplicate records in my application, with the use of Identifier name..
am getting a reply from database in the form of SOAP Message(XML format), through Java, means java is passing an identifier name for that particular application, by using this Identifier only, i have to do the validation, so will u pls tell me how can i do this by using that identifier name...
|
|
|
|
|
You haven't provided anywhere near enough information for us to be a help. What type of application is it? How are you storing your data - for instance, are you using a Dictionary to host the items?
|
|
|
|
|
See am pasting my code here, am creating a keberosed connection to connect to database through Java, the code is like this
KeberosedConnection kc=new KeberosedConnection();
kc.CreateNewReportTemplateType(strIdentifier, "ReportTemplateTypeName", "Description");
am passing the Identifier name through strIdentifier, through which i need to perform validation and perform all the action, related to my application and am passing the report template name(from TextBox) and description through "ReportTemplateTypeName" and "Description" respectively in the above function..
The below code is used to pass the request through SOAP Server to communicate with Database.
public void CreateNewReportTemplateType(string strIdentifier, string ReportTemplateTypeName, string Description)
{
try
{
var sb = new StringBuilder();
sb.Append("<ReportTemplateType xmlns='http://xml.abc.com/ns/abcjava/lrm'>");
sb.Append("<Identifier>");
sb.Append(strIdentifier);
sb.Append("</Identifier>");
sb.Append("<ReportTemplateTypeName>");
sb.Append(ReportTemplateTypeName);
sb.Append("</ReportTemplateTypeName>");
sb.Append("<Description>");
sb.Append(Description);
sb.Append("</Description>");
sb.Append("</ReportTemplateType>");
SoapMessage request = BuildRequest(sb.ToString());
SoapMessage response = ExecuteRequest(request);
}
Assume that the identifier name is "RPNew", so by using this identifier name only,because through Identifier only am communicating with Database, i have to do the validation for checking the duplicate record while am saving the new report...
Hope this will help in Understanding the question better, so shall i expect a answer with a sample..
|
|
|
|
|
First of all, what does this have to do with Silverlight or WPF? Secondly, you are looking to the validation at the wrong location - the only place you can effectively do this is at the database end, which will require the validation to be triggered from that side. You have to do it there because of timing issues - suppose you have two people who both enter the identifier Bob and submit it at the same time; they could both very well pass the validation, only to fail the insert because there is now a conflict.
Rakesh Gowda wrote: so shall i expect a answer with a sample..
Third - that's not the way this site works. We generally don't give full samples here - we help to correct code and to suggest things, not write the whole things ourselves.
|
|
|
|
|
Hi,,
Now i need to export the content(Actually map is the content) which is in the WindowsFormsHost using wpf, i need to export and save that pdf in the given path. So colud any one helf to export the map.
Note: i m using Arcgis server, and using mxd file format data for map.
Now i want to export the pdf file,in button click, and the map should come as it is in the WindowsFormsHost control.
Thanks & Regards
M.Asiq Raja
|
|
|
|
|
Hi experts,
can any one tell me how to generate a tree in silverlight based on the data from database.
My table structure is like this
1- Order table contains, partyCode and orderNo.
2- Item table contains, orderNo & ItemCode.
3- Stages table contains, orderNo, itemCode & stageCode
I want to display tree as below:
Tree display
ord1
Item1
stage1
stage2
Item2
stage1
stage2
stage3
Item3
Item4
Item5
Item6
Item7
Item8
Item9
ord2
……
ord3
…….
I am fetching the data from database from three tables.
|
|
|
|
|
This[^] might be useful to you.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
|
|
|
|
|
Hi!
New to silverlight, i have a problem.
I have created a service to connect to a database and i want to call it many times, the problem is that
string a and string b is executed before the event is triggered?
in the event i have code that catches the result from the service and i want to save it to the string a and then call the same service again and save that result to string b.
but i can't do that if the event isn't triggered each time
any ideas on how to solve?
main(){
serviceclient x = new serviceclient
x.xxx_completed += the event
x.getdataAsync();
string a;
string b;
}
void event ()
{
code to get data
}
modified on Tuesday, January 4, 2011 6:09 AM
|
|
|
|
|
Implement all string saving logic inside the void event() method.
Call the service again after you obtain the result if you have to.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
|
|
|
|
|
I am doing some string operations in the event().
but the problem still is:
in the void event () i define 2 dynamic keys (in a property) depending on the input parameter to the service
after i have done this i use the keys in the other method before calling the service again to retrive two other keys.
but the methodcall_x(keys) is executed before the event is triggered
and keys = null so i can't call the service again.
class x {
MAIN{
Servicecall()
methodcall_x(keys)
}
viod the event(){save result to a property}
}
|
|
|
|
|
What you need to do is to use an Action to return to MAIN once event() method is executed.
Move the methodcall_X(keys) in this Action's handler.
For more info on Actions, check out MSDN.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
|
|
|
|
|
Ok, thanks i will look in to it
|
|
|
|