I have been asked to update a TreeVeiw in a WPF application that is built on the fly from a class object. As you will see treeveiw is not bound to anything. Below is not my code!!
<TreeView Grid.Row="0" HorizontalAlignment="Stretch" Name="tvLocations" VerticalAlignment="Stretch" SelectedItemChanged="tvLocations_SelectedItemChanged" />
private void BuildTreeVeiw(Location locationList)
{
this.Title = _selectedLoc.Name + " - Locations";
tvLocations.Items.Clear();
TreeViewItem tvitem;
tvitem = new TreeViewItem() { Header = locationList.Name, Uid = locationList.Id.ToString() };
if (locationList.Printers != null)
{
TreeViewItem tvprnitem = new TreeViewItem() { Header = "Printers" };
tvprnitem.FontWeight = FontWeights.Regular;
foreach (Printer sprinters in locationList.Printers)
{
TreeViewItem psubitem = new TreeViewItem() { Header = sprinters.Name, Uid = sprinters.Id.ToString() };
TreeViewItem psubitem1 = new TreeViewItem() { Header = String.Concat("UNC: ", sprinters.UNC) };
psubitem.Items.Add(psubitem1);
tvprnitem.Items.Add(psubitem);
}
tvitem.Items.Add(tvprnitem);
}
foreach (Location loc in locationList.Children)
{
AddChildren(loc, ref tvitem);
}
tvLocations.Items.Add(tvitem);
}
private void AddChildren(Location child, ref TreeViewItem tvi)
{
TreeViewItem tvitem;
tvitem = new TreeViewItem() { Header = child.Name, Uid = child.Id.ToString() };
if (child.Name == _currentLocation.Name)
{
tvitem.FontWeight = FontWeights.Bold;
}
if (child.Printers != null)
{
TreeViewItem tvprnitem = new TreeViewItem() { Header = "Printers" };
tvprnitem.FontWeight = FontWeights.Regular;
foreach (Printer sprinters in child.Printers)
{
TreeViewItem psubitem = new TreeViewItem() { Header = sprinters.Name, Uid = sprinters.Id.ToString() };
TreeViewItem psubitem1 = new TreeViewItem() { Header = String.Concat("UNC: ", sprinters.UNC) };
psubitem.Items.Add(psubitem1);
tvprnitem.Items.Add(psubitem);
}
tvitem.Items.Add(tvprnitem);
}
if (child.Children != null)
{
foreach (Location loc in child.Children)
{
AddChildren(loc, ref tvitem);
}
}
tvi.Items.Add(tvitem);
}
This builds the tree correctly and all I have been asked to do is add an icon to the TreeViewItem. The icon will be a different depending on whether it is a location or a printer within that location.
I cannot see how to add icons to TreeViewItems can anyone point me in the right direction?
I solved this by changing this line
tvitem = new TreeViewItem() { Header = child.Name, Uid = child.Id.ToString() };
To
tvitem = GetTreeView(child.Id.ToString(), child.Name, "location.png");
Adding this function
private TreeViewItem GetTreeView(string uid, string text, string imagePath)
{
TreeViewItem item = new TreeViewItem();
item.Uid = uid;
item.IsExpanded = false;
// create stack panel
StackPanel stack = new StackPanel();
stack.Orientation = Orientation.Horizontal;
// create Image
Image image = new Image();
image.Source = new BitmapImage
(new Uri("pack://application:,,/Images/" + imagePath));
image.Width = 16;
image.Height = 16;
// Label
Label lbl = new Label();
lbl.Content = text;
// Add into stack
stack.Children.Add(image);
stack.Children.Add(lbl);
// assign stack to header
item.Header = stack;
return item;
}
Worked perfectly.
Very nice.
I just add this inside method wich return stack panel, to make code much readable:
private StackPanel CustomizeTreeViewItem(object itemObj)
{
// Add Icon
// Create Stack Panel
StackPanel stkPanel = new StackPanel();
stkPanel.Orientation = Orientation.Horizontal;
// Create Image
Image img = new Image();
img.Source = new BitmapImage(new Uri("pack://application:,,/Resources/control.png"));
img.Width = 16;
img.Height = 16;
// Create TextBlock
TextBlock lbl = new TextBlock();
lbl.Text = itemObj.ToString();
// Add to stack
stkPanel.Children.Add(img);
stkPanel.Children.Add(lbl);
return stkPanel;
}
and in treeView Initialization
// Assign stack to header
item.Header = CustomizeTreeViewItem(itemObj);
Related
I've been at this for a while now and cannot seem to figure out how to get the Eto.Forms TreeGridView Control to properly render. I'm trying to just add a few GridViewItem's at the moment and I just get a small gray bar at the top:
Here is my code:
List<ITreeGridItem> treeGridItems = new List<ITreeGridItem>();
foreach (var contentType in contentTypes)
{
treeGridItems.Add(new TreeGridItem(contentType.Name));
}
Content = new DocumentPage(new TreeGridView
{
DataStore = new TreeGridItemCollection(treeGridItems)
}, new Padding(20));
I'm not even really sure where to start, I just want to get a tree with text to show for each node at the moment and I can't even do that.
After a bit of trial and error I figured out how to use the tree view:
var treeGridView = new TreeGridView
{
BackgroundColor = Colors.White
};
treeGridView.Columns.Add(new GridColumn
{
HeaderText = "Content Type",
DataCell = new TextBoxCell(0)
});
treeGridView.Columns.Add(new GridColumn
{
HeaderText = "Create",
DataCell = new CustomCell
{
CreateCell = r =>
{
TreeGridItem item = r.Item as TreeGridItem;
ContentTypeTag tag = (ContentTypeTag)item.Tag;
var contentType = _siteManager.CurrentSite.ContentTypes.First(x => x.Name.Equals(tag.ClassName));
void Click(object btnSender, EventArgs btnArgs)
{
//Your Event
}
var button = new LinkButton
{
Style = "primary-link-btn",
Text = $"Create {contentType.Name.ToSentenceCase()}",
Command = new Command(Click)
};
return button;
}
}
});
treeGridView.Columns.Add(new GridColumn
{
HeaderText = "Show All",
DataCell = new CustomCell
{
CreateCell = r =>
{
TreeGridItem item = r.Item as TreeGridItem;
ContentTypeTag tag = (ContentTypeTag)item.Tag;
var contentType = _siteManager.CurrentSite.ContentTypes.First(x => x.Name.Equals(tag.ClassName));
void Click(object btnSender, EventArgs btnArgs)
{
//Your Event
}
var button = new LinkButton
{
Style = "primary-link-btn",
Text = $"Show All {contentType.Name.ToSentenceCase()}",
Command = new Command(Click)
};
return button;
}
}
});
var treeGridItemCollection = new TreeGridItemCollection();
foreach (var contentType in _siteManager.CurrentSite.ContentTypes)
{
var item = new TreeGridItem
{
Values = new string[] { contentType.Name.ToSentenceCase(), "Create", "Show All" },
Tag = new ContentTypeTag
{
ClassName = contentType.Name
}
};
treeGridItemCollection.Add(item);
}
treeGridView.DataStore = treeGridItemCollection;
You create the header columns to start and then create a TreeGridItemCollection and set the datastore to that. The values for each column of the row is set in a string array to the Values property of the TreeGridItem.
I'm wondering how I can get the contents of a stackpanel which has been added to a GridViewItem (YouTube data API, the stackpanel contains a bitmap image for a thumbnail, the title of the video and a hidden TextBlock with the video ID for reference).
To clarify, I need to be able to obtain the contents of the StackPanel that is contained in the GridViewItem (such as the string of that hidden TextBox) so I can use the data to display the correct video.
Here's the code I use to create the stackpanel.
public async Task SearchByKeyword(string searchTerm)
{
GeneralFunctions gf = new GeneralFunctions();
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = gf.YouTubeAPIKey,
ApplicationName = this.GetType().ToString()
});
var searchListRequest = youtubeService.Search.List("snippet");
searchListRequest.Q = searchTerm; // Replace with your search term.
searchListRequest.MaxResults = 50;
searchListRequest.SafeSearch = SearchResource.ListRequest.SafeSearchEnum.Strict;
// Call the search.list method to retrieve results matching the specified query term.
var searchListResponse = await searchListRequest.ExecuteAsync();
List<string> videos = new List<string>();
List<string> channels = new List<string>();
List<string> playlists = new List<string>();
// Add each result to the appropriate list, and then display the lists of
// matching videos, channels, and playlists.
foreach (var searchResult in searchListResponse.Items)
{
switch (searchResult.Id.Kind)
{
case "youtube#video":
// Create a new StackPanel to insert as a ListViewItem
StackPanel sPanel = new StackPanel();
sPanel.HorizontalAlignment = HorizontalAlignment.Stretch;
sPanel.VerticalAlignment = VerticalAlignment.Stretch;
sPanel.Orientation = Orientation.Vertical;
sPanel.Width = 160;
sPanel.Height = 160;
// Create new StackPanel "Child" elements with alignment and width
TextBlock tb1 = new TextBlock();
tb1.Text = searchResult.Snippet.Title;
tb1.TextWrapping = TextWrapping.WrapWholeWords;
tb1.Width = 160;
// Create new StackPanel "Child" elements with alignment and width
TextBlock tb2 = new TextBlock();
tb2.Text = searchResult.Snippet.ChannelId;
tb2.TextWrapping = TextWrapping.WrapWholeWords;
tb2.Width = 160;
// Create new StackPanel child element for a 120x120 thumbnail of the videos from the search results
Image image = new Image();
image.Source = new BitmapImage(new Uri(searchResult.Snippet.Thumbnails.Default__.Url, UriKind.Absolute));
// Add a "hidden" child element to each stackpanel to hold the video identity
TextBlock h1 = new TextBlock();
h1.Text = searchResult.Id.VideoId.ToString();
h1.Visibility = Visibility.Collapsed;
h1.TextWrapping = TextWrapping.WrapWholeWords;
sPanel.Children.Add(image);
sPanel.Children.Add(tb1);
sPanel.Children.Add(tb2);
sPanel.Children.Add(h1);
SearchResults.Items.Add(sPanel);
break;
case "youtube#channel":
//SearchResults.Items.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.ChannelId));
break;
case "youtube#playlist":
//playlists.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.PlaylistId));
break;
}
}
//Console.WriteLine(String.Format("Videos:\n{0}\n", string.Join("\n", videos)));
//Console.WriteLine(String.Format("Channels:\n{0}\n", string.Join("\n", channels)));
//Console.WriteLine(String.Format("Playlists:\n{0}\n", string.Join("\n", playlists)));
}
Any help is appreciated.
Thanks
I guess I should never code tired right? The answer to my own question is actually very very simple
if (SearchResults.SelectedItems.Count > 0)
{
StackPanel sp = (StackPanel)SearchResults.SelectedItem;
TextBlock tb1 = (TextBlock)sp.Children[1];
TextBlock tb2 = (TextBlock)sp.Children[2];
TextBlock hf1 = (TextBlock)sp.Children[3];
}
can I use only textblock as the only content for transitioning control in mahapps metro. I tried this but it doesn't work as excepted. I am not a wpf expert . Can anyone suggest what is wrong in this? This is what I have tried
StackPanel sp = new StackPanel();
Image imgx = new Image();
Image imgy = new Image();
TextBlock tb = new TextBlock();
if (x == 1)
{
x = 0;
tb.Text = "X = 1";
imgy.Visibility = Visibility.Hidden;
imgx.Visibility = Visibility.Visible;
var uriSource = new Uri("images/blank-cd.png.png", UriKind.Relative);
imgx.Source = new BitmapImage(uriSource);
sp.Children.Add(imgx);
sp.Children.Add(tb);
SecondcustomTransitioning.Content = sp;
}
else if (x == 0)
{
x = 1;
tb.Text = "X = 1";
imgx.Visibility = Visibility.Hidden;
imgy.Visibility = Visibility.Visible;
var uriSource = new Uri("images/placeholder_person.gif", UriKind.Relative);
imgy.Source = new BitmapImage(uriSource);
sp.Children.Add(imgy);
sp.Children.Add(tb);
SecondcustomTransitioning.Content = sp;
}
I have no idea how to put doubleclick events to multiple pushpins that are initialized in the program itself(not in the xaml). The program will get lat and long data from a text file.
here's my current code:
public partial class BingMaps : Window
{
string role;
string nick;
int countLines=0;
public BingMaps(string value,string role2)
{
InitializeComponent();
nick = value;
role = role2;
setCount();
int cLines = 0;
Pushpin[] pushpins = new Pushpin [countLines];
StreamReader z = new StreamReader("dorms.txt");
while (z.Peek() > 0)
{
var pushpinLayer = new MapLayer();
pushpinLayer.Name = "PushPinLayer";
intraMap.Children.Add(pushpinLayer);
string line = z.ReadLine();
string[] temp = line.Split(new char[] { ';' });
var location = new Location(double.Parse(temp[3]), double.Parse(temp[4]));
var pushpin = new Pushpin();
pushpin.Name = "MyNewPushpin";
pushpin.Background = new SolidColorBrush(Colors.Blue);
pushpin.Location = location;
pushpin.ToolTip = "" + temp[0];
pushpins[cLines] = pushpin;
pushpinLayer.Children.Add(pushpins[cLines]);
cLines = cLines + 1;
}
z.Close();
}
public void setCount()
{
using (StreamReader cRead = new StreamReader("dorms.txt"))
{
while (cRead.Peek() > 0) {
cRead.ReadLine();
countLines = countLines + 1;
}
}
}
}
i'm quite new to c# so please bear with me. I need to display different data for each pushpin.
you can register to MouseDoubleClick event
var pushpin = new Pushpin();
pushpin.MouseDoubleClick += (sender, ea) => {
// do something
};
I am programmatically adding items to my Panorama Control called PanoramaCC.
//function to create the panorama items in our view
private void showPanorama(string panoramaName)
{
//create the panorama item and define it
PanoramaItem genItem = new PanoramaItem();
genItem.Height = 265;
genItem.Width = 440;
genItem.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(PanoramaItem_Tap);
genItem.Name = panoramaName;
//create the stackpanel for the panoramaitem
StackPanel genStack = new StackPanel();
genStack.Orientation = System.Windows.Controls.Orientation.Horizontal;
//margin to be done
genStack.Margin = new Thickness(0, -20, 0, 20);
//load the image
Image genImg = new Image();
genImg.Height = 220;
genImg.Width = 400;
genImg.Stretch = System.Windows.Media.Stretch.Fill;
genImg.Margin = new Thickness(20, 5, 20, 5);
string path = "Assets/AppGraphics/CreditCards/" + panoramaName.ToString() + "Front.png";
Uri uriR = new Uri(path, UriKind.Relative);
BitmapImage imgSource = new BitmapImage(uriR);
genImg.Source = imgSource;
//add image into stackpanel
genStack.Children.Add(genImg);
//add stackpanel to the panoramaitem
genItem.Content = genStack;
//add the panoramaitem to the panoramaview
this.PanoramaCC.Items.Add(genItem);
}
The issue I have is that during runtime I want to retrieve the name of the panoramaItem I am currently looking at and do something with it. I've managed to retrieve the name through the tap event for navigation purposes, string name = ((PanoramaItem)sender).Name; but this is a diffrent scenario. I want to retrieve the name and then delete the item with the corresponding name. Pressing a button should delete the currently selected panoramaItem, is what I'm trying to achieve.
You can get the current PanoramaItem by using the SelectedItem property. You don't need to get the name to delete it.
PanoramaItem currentItem = myPanorama.SelectedItem as PanoramaItem;
if(currentItem != null)
{
//if you want the name for other reasons
string name = currentItem.Name;
//Items returns an ItemsCollection object
myPanorama.Items.Remove(currentItem);
}