Is there a way to order stackpanels based on some of its child elements?
In the code behind I add some generic things like groupbox and textblock to a stackpanel, one of the textblocks information is DateTime from my webservice, I have tryed sorting via descending order using linq but the output is still the same.
So I was wondering if its possible to sort by one of the stackpanels child elements namely the textblock1.Text that holds the DateTime attribute?
XDocument xDoc = XDocument.Load(uriGroups);
var sortedXdoc = xDoc.Descendants("Student")
.OrderByDescending(x => Convert.ToDateTime(x.Element("TimeAdded").Value));
foreach (var node in xDoc.Descendants("Student"))
{
GroupBox groupbox = new GroupBox();
groupbox.Header = String.Format(node.Element("StudentID").Value);
groupbox.Width = 100;
groupbox.Height = 100;
groupbox.Margin = new Thickness(1);
TextBlock textBlock = new TextBlock();
textBlock.Text = String.Format(node.Element("FirstName").Value + " " + (node.Element("LastName").Value));
textBlock.TextAlignment = TextAlignment.Center;
TextBlock textBlock1 = new TextBlock();
textBlock1.Text = (DateTime.Parse(node.Element("TimeAdded").Value)).ToString("d");
String.Format("{0:d/M/yyyy}", DateTime.Parse(node.Element("TimeAdded").Value));
textBlock1.TextAlignment = TextAlignment.Center;
textBlock1.VerticalAlignment = VerticalAlignment.Bottom;
StackPanel stackPanel = new StackPanel();
stackPanel.Children.Add(groupbox);
stackPanel.Children.Add(textBlock);
stackPanel.Children.Add(textBlock1);
stackPanel.Margin = new Thickness(5);
stackPanel.MouseEnter += new MouseEventHandler(stackpanel_MouseEnter);
stackPanel.MouseLeave += new MouseEventHandler(stackpanel_MouseLeave);
MainArea1.Children.Add(stackPanel);
}
}
The order of display is totally defined by the order of calls to
MainArea1.Children.Add(stackPanel);
So, try something like
foreach (var node in xDoc.Descendants("Student").OrderBy(e => ...))
{
....
}
(And you really should be using Temlates here)
Related
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];
}
I have seen several questions about doing a custom tooltip for a single line series.
I need a custom tool-tip for each data-point. I would like to add more to the tool-tip than just the dependent value path and the independent value path.
Example I have 2 data points on the same line one with a value(Y-axis)
of 2, date(x-axis) of 4/28/2016, and configuration of A. The other has
a value of 3, date of 4/29/2016, and configuration of B.
How would I also show the configurations? This is all done in code behind because I have a dynamic number of lineseries. So I can't just assign a style to each lineseries in the xaml.
var MyLineSeries = new LineSeries();
lMyLineSeries.DependentValuePath = "Y";
lMyLineSeries.IndependentValuePath = "X";
lMyLineSeries.DataPointStyle = lToolTipDataPointStyle;
This is my code for creating the tool tip style.
var lToolTipDataPointStyle = new Style(typeof(LineDataPoint));
var lTemplate = new ControlTemplate(typeof(LineDataPoint));
var lGridElement = new FrameworkElementFactory(typeof(Border));
//Tooltip
var lStackPanel = new StackPanel();
var lValueContentControl = new ContentControl();
lValueContentControl.SetBinding(ContentControl.ContentProperty, new Binding(myLineSeries.DependentValuePath));
lValueContentControl.ContentStringFormat = "Value: {0}";
var lConfigurationContentControl = new ContentControl();
lConfigurationContentControl.SetBinding(ContentControl.ContentProperty, new Binding())//This is what Idk what to bind to???
lConfigurationContentControl.ContentStringFormat = "Configuration: {0}";
lStackPanel.Children.Add(lValueContentControl);
lStackPanel.Children.Add(lConfigurationContentControl);
lGridElement.SetValue(ToolTipService.ToolTipProperty, lStackPanel);
var lEllipseElement = new FrameworkElementFactory(typeof(Ellipse));
lEllipseElement.SetValue(Ellipse.StrokeThicknessProperty, new TemplateBindingExtension(Border.BorderThicknessProperty));
lEllipseElement.SetValue(Ellipse.StrokeProperty, new TemplateBindingExtension(Border.BorderBrushProperty));
lEllipseElement.SetValue(Ellipse.FillProperty, new TemplateBindingExtension(Grid.BackgroundProperty));
lGridElement.AppendChild(lEllipseElement);
lTemplate.VisualTree = lGridElement;
var lTemplateSetter = new Setter();
lTemplateSetter.Property = LineDataPoint.TemplateProperty;
lTemplateSetter.Value = lTemplate;
lToolTipDataPointStyle.Setters.Add(lTemplateSetter);
return lToolTipDataPointStyle;
I figured it out by using the Tag on the Line series.
myLineSeries.Tag = "Configuration";
var lConfigurationContentControl = new ContentControl();
lConfigurationContentControl.SetBinding(ContentControl.ContentProperty, new Binding(myLineSeries.Tag.ToString()))
lConfigurationContentControl.ContentStringFormat = "Configuration: {0}";
I want to add RowDefinitions to a Grid named notices. I could get the number of rows in the database and iterate those number of times, but I am unable to add the controls to a row.
The row is getting created, but StackPanel is not getting added. My code wants to create rows dynamically and to that a StackPanel and to the StackPanel a label and Image. In the .xaml file, I have only added the Grid.
How can I add a Row with StackPanel to the Grid?
This is my code:
StackPanel sp;
Label dt_label;
Image img;
BitmapImage bitmap;
string col1=null, col2 = "Picture_Path";
string Picture_Path=null;
RowDefinition gridRow1;
DateTime time;
for (int row_num = 0; row_num < No_Of_Rows; row_num++)
{
DataRow row = dt1.Rows[row_num];
foreach (object item in row.ItemArray)
{
gridRow1 = new RowDefinition();
sp = new StackPanel();
sp.Background = new SolidColorBrush(Colors.Yellow);
col1 = "Date_Time".ToString(); ;
time = DateTime.Parse(dt1.Rows[row_num][col1].ToString());
dt_label = new Label();
dt_label.Content = time;
Picture_Path = dt1.Rows[row_num][col2].ToString();
img = new Image();
bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.UriSource = new Uri(Picture_Path, UriKind.Relative);
bitmap.EndInit();
img.Source = bitmap;
img.Height = 100;
img.Width = 100;
sp.Children.Add(dt_label);
sp.Children.Add(img);
notices.RowDefinitions.Add(gridRow1);
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show("ERROR \n" + ex.ToString());
}
You must set attached properties (Grid.Row and Grid.Column) to your controls using Grid.SetRow and Grid.SetColumn.
After this you need to add control to grid like this:
nameOfYourGrid.Children.Add(nameOfYourControl);
Also, there are some interesting lines in your code:
"Date_Time".ToString(); ;
Why are you converting string to a string?
Majority of fields can be moved to inner foreach scope.
Im having diffculty with datetime when its displayed client side from my rest web service, my client side wpf app code looks like this:
public MainWindow()
{
InitializeComponent();
string uriGroups = "http://localhost:8000/Service/Student";
XDocument xDoc = XDocument.Load(uriGroups);
foreach(var node in xDoc.Descendants("Student"))
{
GroupBox groupbox = new GroupBox();
groupbox.Header = String.Format(node.Element("StudentID").Value);
groupbox.Width = 100;
groupbox.Height = 100;
groupbox.Margin = new Thickness(2);
TextBlock textBlock = new TextBlock();
textBlock.Text = String.Format(node.Element("FirstName").Value + " " + (node.Element("LastName").Value));
textBlock.TextAlignment = TextAlignment.Center;
TextBlock textBlock1 = new TextBlock();
textBlock1.Text = String.Format(node.Element("TimeAdded").Value);
textBlock1.TextAlignment = TextAlignment.Center;
textBlock1.VerticalAlignment = VerticalAlignment.Bottom;
StackPanel stackPanel = new StackPanel();
stackPanel.Children.Add(groupbox);
stackPanel.Children.Add(textBlock);
stackPanel.Children.Add(textBlock1);
stackPanel.Margin = new Thickness(10);
MainArea.Children.Add(stackPanel);
}
}
And my service looks like this:
public class Student
{
....
public DateTime TimeAdded;
public string TimeAddedString
{
get
{
return this.TimeAdded.ToString("dd/MM/yyyy hh:mm:ss");
}
}
But the output looks like this:
Is there a way on my client side app code to truncate this or reformat it?
You can cast it to a DateTime and then use String.Format
Here is an example with one format you could use:
String.Format("{0:M/d/yyyy}", ((DateTime)node.Element("TimeAdded").Value))
You can also use DateTime.ToString(FORMAT)
((DateTime)node.Element("TimeAdded").Value).ToString("d");
I have made an assumption that .Value returns an object, but if it returns a DateTime then you can drop the casts.
If you are getting a string into your client, then you will need to use DateTime.Parse
(DateTime.Parse(node.Element("TimeAdded").Value)).ToString("d");
String.Format("{0:M/d/yyyy}", DateTime.Parse(node.Element("TimeAdded").Value))
You are using TimeAdded...but I think you should be using TimeAddedString
textBlock1.Text = String.Format(node.Element("TimeAdded").Value);
Should be
textBlock1.Text = String.Format(node.Element("TimeAddedString").Value);
I believe
I am trying to create a generic view, and I want it to contain a ListBox with a datatemplate
I want to create it either using pure C# code or if possible load it through xaml? If I can create a template I can get in c# as a resource of sorts.
What I have made until now is
private static ListBox CreateDayListBox()
{
var listBox = new ListBox();
var dataTemplate = new DataTemplate();
var grid = new Grid();
var columnDefinition1 = new ColumnDefinition {Width = GridLength.Auto};
var columnDefinition2 = new ColumnDefinition();
grid.ColumnDefinitions.Add(columnDefinition1);
grid.ColumnDefinitions.Add(columnDefinition2);
var rectangleItemBought = new Rectangle {Width = 50, Height = 50};
rectangleItemBought.SetBinding(Rectangle.FillProperty, new Binding("Bought"));
grid.Children.Add(rectangleItemBought);
var textBlockItemName = new TextBlock();
textBlockItemName.SetBinding(TextBlock.TextProperty, new Binding("Name"));
var textBlockItemQuantity = new TextBlock();
textBlockItemQuantity.SetBinding(TextBlock.TextProperty, new Binding("Quantity"));
var textBlockItemQuantityType = new TextBlock();
textBlockItemQuantityType.SetBinding(TextBlock.TextProperty, new Binding("QuantityType"));
var stackpanel = new StackPanel();
Grid.SetColumn(stackpanel, 1);
stackpanel.Children.Add(textBlockItemName);
stackpanel.Children.Add(textBlockItemQuantity);
stackpanel.Children.Add(textBlockItemQuantityType);
grid.Children.Add(stackpanel);
return listBox;
}
So I want the listbox datatemplate to contain 1 rectangle, 1 stackpanel with 3 textboxes inside
You can write the template in XAML then load it in your code.
Read this.
Besides, I'm pretty sure you can create the DataTemplate by code the same way you created the controls, look at this code (credit):
DataTemplate template = new DataTemplate();
FrameworkElementFactory factory =
new FrameworkElementFactory(typeof(StackPanel));
template.VisualTree = factory;
FrameworkElementFactory childFactory =
new FrameworkElementFactory(typeof(Image));
childFactory.SetBinding(Image.SourceProperty, new Binding("Machine.Thumbnail"));
childFactory.SetValue(Image.WidthProperty, 170.0);
childFactory.SetValue(Image.HeightProperty, 170.0);
factory.AppendChild(childFactory);
childFactory = new FrameworkElementFactory(typeof(Label));
childFactory.SetBinding(Label.ContentProperty,
new Binding("Machine.Descriiption"));
childFactory.SetValue(Label.WidthProperty, 170.0);
childFactory.SetValue(Label.HorizontalAlignmentProperty,
HorizontalAlignment.Center);
factory.AppendChild(childFactory);