Binding in WPF Toolkit on Chart Series - wpf

it's my first post here so, sorry for my mistakes in advance:). Here is my problem: I am trying to build a WPF application using the MVVM pattern and I'm not ALWAYS able to load data in my chart. The only time when it works is when I use the PieSeries. In the all other cases I receive an error. What is strange is that in my designer window is amable to see my chart.
Here is my code:
<Border Background="White" BorderThickness="2" BorderBrush="Black" Grid.Column="1">
<!--ContentPresenter Content="{Binding Path=FormHost}">
</ContentPresenter-->
<Grid>
<chart:Chart Name="chart1">
<chart:Chart.Series>
<chart:PieSeries Name="series1" ItemsSource="{Binding ChartItems}"
IndependentValueBinding="{Binding Path=Name}"
DependentValueBinding="{Binding Path=Value}"/>
</chart:Chart.Series>
</chart:Chart>
</Grid>
</Border>
In the viewModel I have among other things:
private ObservableCollection<ChartElement> chartItems;
public MainWindowViewModel()
{
chartItems = new ObservableCollection<ChartElement>();
chartItems.Add(new ChartElement("da011111111111", 3));
chartItems.Add(new ChartElement("adas111111", 490));
chartItems.Add(new ChartElement("adas111111", 341));
chartItems.Add(new ChartElement("adas111111", 413));
chartItems.Add(new ChartElement("adas111111", 143));
}
Image with the error
Thank you in advance.

It seems that the problem is in the WPF Toolkit. The bindings above were correct. The problem was that I had specified the xml:lang in my window other than en-US and because of that the application crashed.
For more details please see here and here

Related

Avalon dock DocumentTitleTemplate vs DocumentHeaderDataTemplate

Can you please explain the difference between DocumentHeaderDataTemplate and DocumentTitleTemplate in avalonDock.
They are properties of DockingManger, but I want to be sure how to use them.
<xcad:DockingManager DocumentsSource="{Binding OpenViewers}"
x:Name="MainTabControl"
LayoutItemTemplate="{StaticResource imageSelectionTemplate}"
ActiveContent="{Binding ActiveTabItem, Mode=TwoWay}"
DocumentHeaderTemplate="{StaticResource DocumentHeaderDataTemplate}"
DocumentTitleTemplate="{StaticResource DocumentTitleDataTemplate}"
IsHitTestVisible="True"
VerticalContentAlignment="Stretch"
VerticalAlignment="Stretch">
DocumentTitleTemplate is use in the Template of FloatingDocumentWindow.
DocumentHeaderTemplate is used in the Template of LayoutDocumentTabItem.

Defining CustomMessageBox from Windows Phone Toolkit in XAML

Can I define CustomMessageBox in XAML? I have the code:
<phone:phone:PhoneApplicationPage.Resources>
<toolkit:CustomMessageBox x:Key="CustomMessageBox" Title="Blabla" IsLeftButtonEnabled="True" LeftButtonContent="OK" Content="blabla" />
</phone:PhoneApplicationPage.Resources>
And when I'm trying to run it:
(this.Resources["CustomMessageBox"] as CustomMessageBox).Show();
I get InvalidOperationException - "Element is already the child of another element.".
Is it possible to do it this way or I have to define it from code-behind? Is there any workaround?
Due to this book (not advertizing, just showing source), you can do follow:
1 Create new UserControl, name it CustomUserControl.xaml
2 Add custom UI elements to UC
<StackPanel x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}" Margin="0,0,0,10">
<TextBlock Text="Custom content inside the UserControl XAML" TextWrapping="Wrap"
HorizontalAlignment="Left"/>
<MediaElement Source="http://mschannel9.vo.msecnd.net/o9/mix/09/wmv/key01.wmv"/>
</StackPanel>
3 Run from codebehind of your main page
var messageBox = new CustomMessageBox
{
Content = new CustomUserControl(),
LeftButtonContent = "OK",
RightButtonContent = "Cancel",
};
messageBox.Show();
Seems now you can play video in message box :)
PS: also found some info here

How to keep bindings when serializing a WPF usercontrol into an XElement

I'm trying to serialize a custom WPF user control that features a grid with a textblock that is bound to a dependency property named "Frequency". The snippet that defines the textblock is as follows (the definition of the user control is quite lengthy to post it here!):
<TextBlock x:Name="FrequencyText" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"
FontFamily="Arial Rounded MT"
Foreground="White" FontSize="10" FontWeight="DemiBold" Margin="3"
TextBlock.Text="{Binding Frequency, ElementName=FrequencyButtonControlA, Path=Frequency}"
TextAlignment="Center" VerticalAlignment="Center" HorizontalAlignment="Left" />
I'm working on an editor tool that enables the user to arrange some custom controls into a canvas and change their properties and I need to provide the user with the capability to clone an existing element. In order to do this, I'm serializing the control into an XElement and instantiating a new one afterwards. The code to perform the serialization is as follows:
var contentXaml = (commsPanelItem.Content != null) ? XamlWriter.Save(commsPanelItem.Content) : string.Empty;
var serializedItem = new XElement(
"CommsPanelItem",
new XElement("Type", commsPanelItem.GetType().ToString()),
new XElement("Left", Canvas.GetLeft(commsPanelItem.UIElement)),
new XElement("Top", Canvas.GetTop(commsPanelItem.UIElement)),
new XElement("Width", commsPanelItem.Width),
new XElement("Height", commsPanelItem.Height),
new XElement("zIndex", Panel.GetZIndex(commsPanelItem.UIElement)),
new XElement("Content", contentXaml),
commsPanelItem.GetAttributesForSavingPanelInConfigurator() as XElement);
return serializedItem;
The problem I'm having is that, when serializing the textblock, the "Text" field doesn't preserve the binding to the "Frequency" dependency property, it just gets exported with the value assigned by the user ("131.5" in the following example):
TextBlock Text="131.5" FontFamily="Arial Rounded MT" FontWeight="SemiBold" FontSize="10" Foreground="#FFFFFFFF" TextAlignment="Center" Name="FrequencyText" Margin="3,3,3,3" HorizontalAlignment="Left" VerticalAlignment="Center" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2"
My question is: is there any way to be able to serialize dependency property's binding instead of the actual value?
Thanks in advance!
Use the "GetBinding" method found on FrameworkElements instead of pulling its value:
BindingExpression bindingExpression = myTextBox.GetBindingExpression(TextBox.TextProperty);
Then you can probably serialize the BindingExpression.

Client side report generating and printing in Silverlight

I have been working on a project that needs to produce a simple report containing a header, a table with simple grouping and a footer. this report needs a print functionality and it can be more than one page.
I found it really difficult to create this report using DataGrid since I can't generate and print more than one page PrintDocument .
So far I tried to use iframe (using HTMLPlaceHolder by Telerik) with html report which I generate using silverlight code behind but javascript printing function prints the entire silverlight page.
I have telerik and I use it for advance reports but I don't want to use telerik report for this specific report since the report is generating on the server(I don't want to pass any value back to server at all for this report).
Is there any way to generate such a report in client side with printing functionality.
I am open to all suggestion as long as it's not too expensive (up to $100)
Let me know if you need more detail.
You might want to use PrintDocument class in Silverlight. The usage is like..
in XAML file create List as
<ScrollViewer Height="300" VerticalScrollBarVisibility="Auto">
<ItemsControl x:Name="printSurface">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal"
Height="25">
<TextBlock Width="100"
Text="{Binding Name}" />
<TextBlock Width="75"
Text="{Binding Genre.Name}" />
<TextBlock Width="50"
Text="{Binding Price, StringFormat=c}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
and Code behind looks like
void printButton_Click(object sender, RoutedEventArgs e)
{
PrintDocument doc = new PrintDocument();
doc.PrintPage += new EventHandler<PrintPageEventArgs>(doc_PrintPage);
doc.Print("Page title");
}
void doc_PrintPage(object sender, PrintPageEventArgs e)
{
// Stretch to the size of the printed page
printSurface.Width = e.PrintableArea.Width;
printSurface.Height = e.PrintableArea.Height;
// Assign the XAML element to be printed
e.PageVisual = printSurface;
// Specify whether to call again for another page
e.HasMorePages = false;
}

If you were going to build an org chart builder in Silverlight, what base class would you use for creating the boxes of the chart?

For this question, let us assume that we will want to show the face of the employee, title, department, and whether they like PiƱa coladas/getting caught in the rain.
Perhaps it might look something like the following:
http://www.edrawsoft.com/images/examples/Photo-Org-Chart-Full.png
Would you use a...
System.Windows.Control.UserControl?
FrameworkElement?
UIElement?
Canvas
Why? As always, thank you for your advise! I greatly appreciate it!
If I had to create a org chart control with advanced layout I would probably derive from Control, and create a "real" templated control in a similar manner as e.g. the TreeView control. This is probably the most advanced route to create a new control, but also the most powerful.
You may also be able to modify the control template of a TreeView, and make it grow downwards from the center instead of left and down from the upper left corner, but it will probably be difficult or impossible to customize the layout of the various levels as the TreeViewItem doesn't carry any extra information to describe the layout of a particular node.
In fact I did recently some experiments modifying the TreeView control template, but I stumbled upon something I didn't understand. Luckily I figured out what I did wrong, and you can see how it is possible to change the orientation of TreeView child items from vertical to horizontal in my question here on Stack Overflow.
I've seen a website that uses TreeViewItem and ControlTemplates, but I can't find it at the moment, I think it was on CodeProject.
Another idea I was playing with recently is use 2 usercontrols, itemcontrols and stackpanels.
Here's an example of a an OrgBar rectangle with text under it and it renders it's children in OrgGroup control by setting the ItemSource to it's children collection recursively. You can put the root orgbar on a canvas and play around with paths for the arrows. I tried to point out the basics but if you need more I can fill in the blanks.
Public Class OrgBarDataNode
Public Property BarColor as New SolidColorBrush(Colors.Red)
Public Property BarName As String
Public Property Children as New ObservableCollection(Of OrgBarDataNode)
End Class
Class MainPage
...
Public Sub Loaded
Dim Root as New OrgBarDataNode With {.BarName = "Root"}
Dim Child1 as New OrgBarDataNode With {.Barname = "Child1"}
Root.Children.Add(Child1)
LayoutRoot.Children.Add(Root)
End Sub
...
End Class
<UserControl x:Class="OrgBar">
<Grid>
<StackPanel ToolTipService.ToolTip="{Binding BarName}" Cursor="Hand">
<Rectangle Fill="{Binding BarColor}" Style="{StaticResource RecStyle}"/>
<TextBlock Text="{Binding BarName}" HorizontalAlignment="Center"
Margin="0,10,0,0" />
<local:OrgGroup Margin="0,20" HorizontalAlignment="Center"
DataContext="{Binding Children}" />
</StackPanel>
</Grid>
</UserControl>
<UserControl x:Class="OrgGroup">
<Grid>
<!-- this {Binding} to nothing means bind to DataContext}-->
<ItemsControl ItemsSource="{Binding}" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:OrgBar Style="{StaticResource OrgBarStyle}"
DataContext="{Binding}" />
<!-- this {Binding} refers to the the child node this time} -->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Grid>
</UserControl>
That's essentially a tree structure, so like Paully suggested, I would start with a TreeView (Silverlight Toolkit) and customize the control template and treeview itself.

Resources