Hi am adding new tree view items in a rad treeview programatically like below
but the image is not visible in the treeview .It only displays if i gave the full path of my solution .But its not possible for real time.I need to map the images from the resources folder of my project .How to achieve it..??
6 Answers, 1 is accepted
When you define a string path in code that points to a file included in the root folder of the project you will need to get two directories back. So, the following syntax should find your Resources folder if its located in the project's main folder.
Note that in some cases the image might not be displayed until you Rebuild the project.
I hope this helps.
Without your implementation I cannot tell why the image is not displayed. However, I can suggest you another syntax for the image path which you can try.
Where "MyAssemblyName" is the name of the assembly where the image is stored. By default the name of the project matches the assembly name. So, you can change "MyAssemblyName" in the code snippet with the name of your project.
The word "component" is a key word which should stay the same. And after this you can specify the path to the image.
You can also take look at the Pack URIs in WPF MSDN article which demonstrates another way of constructing a path to a file in an application.
How can i attach my sample soluition here...?? it only supportssome image format in the attachment..!!
Hi here is the code which am trying to make it work.Can you please check it..
I am extending the base class (RadTreeViewItem ) and using it.
Check the code in the ViewModel.cs of the attached project.There
DefaultImageSrc=@"C:\Users\SPK\Documents\WPF Samples\Treeview class extend\Resources\Object.png"
works fine .But DefaultImageSrc = @"../../Resources/Object.png" is not working..!!
Here is my codes
3Classes: CustomTreeView.cs ,CustomTreeViewItem.cs and ViewModel.cs
1 Windows :MainWindow.xaml
CustomTreeView class code
and here is the code for main window.
and window.xaml.cs page code
The forums allow to attach only image files. To send a zip file you can open a new support ticket. However, I used your code to assemble a working project.
I noticed few things in your implementation that I want to emphasize on. The ItemsSource property of the RadTreeView control, or any other ItemsControl in the WPF framework, is designed for data binding scenarios. And it expects a collection of business objects that describe the RadTreeViewItems. In your case the ItemsSource is populated directly with visual elements (custom RadTreeViewItems) which is not recommended. You can read more about data binding in the Populating with Data section in the RadTreeView help documentation.
So, when the ItemsSource is populated the items control (RadTreeView in this case) will create a RadTreeViewItem container for each business object in the collection. This happens in the GetContainerForItemOverride() method of the treeview. Then you will need to bind the properties of the generated RadTreeViewItems to the object from the ItemsSource collection.
About the missing image, I was not able to reproduce this, the image is displayed properly when using relative path. Keep in mind that the RadTreeViewItem with the image will be wrapped into the automatically generated RadTreeViewItem's Header, which is not quite expected behavior.
In order to resolve this, instead of populating the ItemsSource with RadTreeViewItems you can create a model that holds information about the visual items (including the path to the image) and bind its properties using a Style. I attached a sample project demonstrating this approach. Please give it a try and let me know if it helps.