WPF MVVM Chartplotter: Dynamically bind multiple Lines - c#

I'm trying to make the WPF Chartplotter work with some WPF MVVM. As a target of my interest I've chosen our TeamCity Tests Performance. The current Plotter is looking like this:
<dd:ChartPlotter Grid.Row="1">
<dd:ChartPlotter.HorizontalAxis>
<dd:HorizontalIntegerAxis />
</dd:ChartPlotter.HorizontalAxis>
<dd:ChartPlotter.VerticalAxis>
<dd:VerticalIntegerAxis/>
</dd:ChartPlotter.VerticalAxis>
<dd:VerticalAxisTitle Content="Performance in Minutes" FontSize="10" />
<dd:HorizontalAxisTitle Content="Build by ID" FontSize="10" />
<dd:LineGraph DataSource="{Binding TestRunsPerformance, IsAsync=True}" Stroke="Green" StrokeThickness="2">
<dd:LineGraph.Description>
<dd:PenDescription DescriptionString="Test" />
</dd:LineGraph.Description>
</dd:LineGraph>
<dd:LineGraph DataSource="{Binding TestSlowRunsPerformance, IsAsync=True}" Stroke="Red" StrokeThickness="2">
<dd:LineGraph.Description>
<dd:PenDescription DescriptionString="TestSlow" />
</dd:LineGraph.Description>
</dd:LineGraph>
</dd:ChartPlotter>
</Grid>
So I have basically two Test-Categories and for each I show a LineGraph in the Plotter, which I bind to the EnumerableDataSource in the ViewModel. Unfortunately I couldn't test it, but I'm certain it will work this way, since one LineGraph did work so far.
This is cool, but now I would like to make a second Chart with the Unit-Tests with the highest influence. But for this, I can't just create LineGraphs in the XAML and bind, but I would need something like a LineGraph Collection and set an amount of LineGraphs dynamically. Since I'm also learning MVVM, it would be troublesome If I'd really need to do this in the Code behind.
Unfortunately, all the Examples I found so far do exactly this, for example: Dynamic Line chart in C# WPF Application
I found also examples to make this work for Canvas: Is it possible to bind a Canvas's Children property in XAML? but these are UIElements, not PlotterElements.
Is what I'm trying even possible or am I doing something fundamentally wrong?

Related

Why WPF XAML front end looks diffrent on other machines?

I wrote a WPF C# application and every element of front end is adjusted in margins, for example:
<TabItem x:Name="ClipboardItem" FontSize="15" FontFamily="Century Gothic" Height="60" Header="
Multi Clipboard" Background="#2a2a29" Foreground="#e7e8ea" Margin="-4,-7,-57,8">
On my computer, everything looks good and just how i wanted. Weird thing is about other PCs. For example on other PC2 element let's call it A looks diffrent and run over but on other PC3 element A looks exactly like on my PC but other element B looks totally diffrent. To the point, on other PCs some elements are just like they should be, but some elements are not. It's window application with rigidly set height and length. Could you tell me how to fix that?
First of all. computers can come in many different resolutions and aspect ratios. so if you want to fit them all. use GridView or a ViewBox (there's more but these are good for starters). and it's not recommended to use margins for most elements unless it's small and in a corner then again don't use that.
use something suitable instead of HorizontalAlignment="Stretch"
<GridView HorizontalAlignment="Stretch" >
<TabItem x:Name="ClipboardItem" FontSize="15" FontFamily="Century Gothic"
Height="60" Header=" Multi Clipboard" Background="#2a2a29"
Foreground="#e7e8ea">
</GridView>
Learn about Grid Views before you try this code https://learn.microsoft.com/en-us/dotnet/desktop/wpf/controls/gridview-overview?view=netframeworkdesktop-4.8
if you want to maintain the aspect ratio Use View Box:
https://stackoverflow.com/a/290229/10518289
One other thing to keep in mind that all computers you test on must run the same version of .Net Framwork or the latest.

C# wpf - Adding to xaml file during execution

This is what my main program GUI will look like, what I am attempting to do, is to create a reminder application in C# using wpf.
I am going to use a scroll viewer which is going to be displaying the data to the user, namely the reminders they currently have.
Each time the user adds a reminder, it will add this:
What I am wanting to do is that, when ever the user add's a new reminder, there will be a new set of data added to the scrollviewer.
What would be the best way of achieving this?
Am I able to hold the xaml data and add it during execution?
Thanks for the help
What you want to do can be accomplished not by dynamic Xaml, but by the use of a templated control which can accept dynamic data. For example you wouldn't consider using a listbox for your labels because you are not showing the data in a list right?
But a listbox is just a conveyor belt for what you want to achive. Say you want more than a label, how about three labels. Via binding to a proper structure you can get what is needed.
Here is an example
<ListBox ItemsSource="{Binding myReminders }">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=ReminderName}" />
<TextBlock Text="{Binding Path=Description}"/>
<TextBlock Text="{Binding Path=Priority}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Once that is bound to a list of my reminder data objects (which the list can dynamically change), we have the ability to show the reminders in any format we want. One just uses the above to style it appropriately. (Check out WPF's Templating Overview for a great example).
The use of templates is done in other controls, so if the listbox is not to your liking, look into other templated controls.

Binding with an anonymous object on WP7.5

On WP7.5 I created a datacontext like this:
this.DataContext = new { One = listOne, Two = listTwo};
On my XAML code I tried this:
<TextBlock Text="{Binding listOne.m_strTypeTiers}" Style="{StaticResource PhoneTextNormalStyle}" />
<TextBlock Text="{Binding listTwo.m_strTypeTiers}" Style="{StaticResource PhoneTextNormalStyle}" />
My textbox are empty. I think my binding syntax has a mistake.
I think you are looking for {Binding One.m_strTypeTiers}
But it depends on what listOne and listTwo are. Are they classes with a property named m_strTypeTiers? If m_strTypeTiers is a field, then you will not be able to databind to it. Databinding only works with properties (by default).
UPDATE
Oops. Silverlight does not support binding to anonymous types. I was thinking of WPF for desktop applications. Windows Phone 7 uses Silverlight which is very similar but different in some important ways. I think you are out of luck - you will need to define a concrete class.
You may find this article helpful in the future:
Contrasting Silverlight and WPF (...not that it would have helped you with this particular question)

Dynamically Adding child controls to a silverlight textbox

Please forgive this stupid question. (I'm originally an ASP.NET programmer.)
I'm trying to add a telerik context menu to a textbox control in the code behind.
Adding it in the xaml is very easy (this works)
<TextBox AcceptsReturn="True" Text="{Binding Mode=TwoWay, Path=Description}" TextWrapping="Wrap" x:Name="txtIssues" Width="280" Height="100" VerticalScrollBarVisibility="Auto">
<telerikNavigation:RadContextMenu.ContextMenu>
<telerikNavigation:RadContextMenu x:Name="contextMenu"
ItemClick="ContextMenuClick">
<telerikNavigation:RadMenuItem Header="Set Vista as Background" />
<telerikNavigation:RadMenuItem Header="Set Beach as Background" />
<telerikNavigation:RadMenuItem Header="Set Forest as Background" />
</telerikNavigation:RadContextMenu>
</telerikNavigation:RadContextMenu.ContextMenu>
</TextBox>
However I would like to completely add the the control from c# code and I can't find a why to add a control to a textbox. I've been looking for something like "txtIssues.Children.Add" but there doesn't seem to be an option.
First its best you understand that you are not adding a control to the TextBox. The RadContextMenu.ContextMenu is not a control it is an attached property.
Funnily enough the Telerik documentation describes adding a context menu to a textbox in C#. See Working with the RadContextMenu. Sometimes "RTM" is actually good advice.

Best practices for designing a simple element to be used in many places, WPF

What are the best practices for designing a simple component, for example a border with a background, rounded corners and a textblock with specific styling inside? What I need to be able to do is add this component on many different objects (basically a styled label for the items). The easiest way to design such a thing, in my opinion, is via XAML, but how do I create more of these objects from the code behind?
Another option would of course to just write it all in code, but it is much slower to design the look by just looking at code. I tried googling around a bit but I suppose I am simply not figuring out the correct keywords because I was unable to find anything of use.
I think there are multiple ways of doing this. Depends on what exactly you want to achieve. You would want to read up on the following in WPF
1. UserControls
2. CustomControls
3. Styles
4. Templates
5. Resources
You could use a ContentControl and set its template. Your template would be the border/background/rounded corners etc...
<DataTemplate x:Key="MyTemplate">
<Border>
...
<TextBlock Text="{TemplateBinding Content}" />
...
</Border>
</DataTemplate >
You'd use it like this:
<ContentControl ContentTemplate="{StaticResource MyTemplate}" Content="blah blah" />

Categories

Resources