Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am new in WPF and C# and I am adding controls in a WPF app dynamically.

I tested my code with a windows slider first and it works but When I try to add a custom control didn't works. I am using the Agauge Circular gauge custom control for Silverlight 3 and WPF[^]

I used this control in other apps and works perfctly. To add dynamically the controls I am following this tutorial http://10rem.net/blog/2010/12/08/dynamically-generating-controls-in-wpf-and-silverlight[^]

I try in more than one whay

First I try to add the gauge like this:

C#
public partial class DynamicUI : UserControl
{
       
   List<CircularGaugeControl> gauges_list = new List<CircularGaugeControl>();
   int flag;
   public DynamicUI()
   {
      InitializeComponent();
 
      Loaded += new RoutedEventHandler(MainPage_Loaded);
   }

   void MainPage_Loaded(object sender, RoutedEventArgs e)
   {
      CreateUIGauges();
   }

   private void CreateUIGauges()
   {
      UniformGrid ug = new UniformGrid();
      flag = 3;
      while (flag  != 0)
      {        
         CreateCircularGaude();
         flag--;
      }

      foreach (CircularGaugeControl obj in gauges_list)
      {
         ug.Children.Add(obj);
      }
      rootGrid.Children.Add(ug);
      LayoutRoot.Children.Add(rootGrid);
   }

   private void CreateCircularGaude()
   {
      CircularGaugeControl cg = new CircularGaugeControl();
      cg.MaxValue = 500;
      cg.MinValue = 0;
      cg.Radius = 100;
      cg.ScaleLabelRadius = 80;
      cg.ScaleStartAngle = 300;
      cg.DialText = "RPM";
      cg.PointerThickness = 9;
      cg.FontSize = 10;
      cg.ScaleStartAngle = 140;
      cg.ScaleSweepAngle = 200;
      gauges_list.Add(cg);
   }
}

It doesn't work so Then I am trying to add the agauge in XALM but from .cs:


XML
private void CreateCircularGaude()
{
   //Option a) XALM ELEMENT 
   string xaml=  
      "<Grid Margin='10' " +
      "xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' " + 
      "xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' " + 
      "xmlns:gauge='clr-namespace:CircularGauge;assembly=CircularGauge'>"+ 
      "<!--DarkSlateGray gauge--> "+     
      "<gauge:CircularGaugeControl x:Name='myGauge4' Grid.Column='1' Grid.Row'1'"+
      " Radius='150'"+
      "ScaleRadius='110'"+ 
      "ScaleStartAngle='120'" +
      "ResetPointerOnStartUp='True'"+
      "ScaleSweepAngle='300'"+
      "PointerLength='85'"+ 
      "PointerCapRadius='35'"+ 
      "MinValue='0'"+ 
      "MaxValue='1000'"+ 
      "MajorDivisionsCount='10'"+ 
      "MinorDivisionsCount='5'"+ 
      "CurrentValue='{Binding Score}'"+
      "ImageSource='windowslogo.png'"+
      "ImageSize='40,50'"+
      "RangeIndicatorThickness='0'"+
      "RangeIndicatorRadius='0'"+
      "ScaleLabelRadius='90'"+
      "ScaleLabelSize='40,20'"+
      "ScaleLabelFontSize='11'"+
      "ScaleLabelForeground='Black'"+
      "MajorTickSize='10,3'"+
      "MinorTickSize='3,1'"+
      "MajorTickColor='DarkGray'"+
      "MinorTickColor='DarkGray'"+
      "ImageOffset='-50'"+
      "GaugeBackgroundColor='DarkSlateGray'"+
      "PointerThickness ='12'"+
      "OptimalRangeStartValue='300'"+
      "OptimalRangeEndValue='700'"+ 
      "DialTextOffset='40'"+
      "DialText='Slate Gray'"+
      "DialTextColor='DarkSlateGray'>" + 
      "</gauge:CircularGaugeControl>"+
      "</Grid >";

      StringReader stringReader = new StringReader(xaml);
      XmlReader xmlReader = XmlReader.Create(stringReader);
      UIElement tree = (UIElement)XamlReader.Load(xmlReader);
      LayoutRoot.Children.Add(tree);
}


The app breaks and print this error message:
+	$exception	{"''' is an unexpected token. The expected token is '='. Line 1, position 302."}	System.Exception {System.Windows.Markup.XamlParseException}
but I don't see any parsing error.

Do you know a better way to do this? or why is this error happening?
Posted
Updated 10-Sep-14 22:32pm
v7
Comments
ashok rathod 9-Sep-14 7:09am    
will suggest please copy your xaml code and paste it in new window. it will tell what exact error you are getting.
Vincent Beek 10-Sep-14 2:26am    
What happens if you move your gauge creation code to the constructor instead of the page loaded event? And what is UG, grid?
Kinna-10626331 10-Sep-14 2:36am    
If I add the code in the constructor instead in the loaded event , it print the same errors . UG is a uniform grid where display the controls. Then I add this UniformGrid to the global Grid. It works with windows controls like sliders or progressbars.

1 solution

Open the circularGaugeDemoWPF project in the customcontrol that you downloaded from this site.

Remove the 4 controls from the Window1.XAML.
Remove the code from the window1_loaded event in codebehind.
Add using CircularGauge;
Add to constructor:

CircularGaugeControl cg = new CircularGaugeControl();
cg.MaxValue = 500;
cg.MinValue = 0;
cg.Radius = 100;
cg.ScaleLabelRadius = 80;
cg.ScaleStartAngle = 300;
cg.ScaleLabelFontSize = 12;
cg.DialText = "RPM";
cg.PointerThickness = 9;
cg.FontSize = 10;
cg.ScaleStartAngle = 140;
cg.ScaleSweepAngle = 200;
LayoutRoot.Children.Add(cg);


This is not a copy from your code!
This works on my station.
 
Share this answer
 
Comments
Kinna-10626331 10-Sep-14 4:41am    
Thanks Vincent , now it works for me :), I don't understand what was exactly the problem , maybe some trouble with declarations I don't know. If you know it please , share it because I would like to get it. Anyway thank you so much :)
JulesKo 10-Sep-14 9:58am    
Maybe it was this: "ScaleRadius '110'". It should probably be "ScaleRadius='110'" ?
Kinna-10626331 11-Sep-14 4:37am    
Yess, I fix it and I revise it again but the code still reporting parse errors. The curious things are: 1) I tried to add the XALM code from the tutorial "Dynamically generating controls" and this code also reports parsing errors. 2) I tried to add a simple button in XALM code to test this feature and it also reports xalm parsing error. The truth is that I don't know why I can not load XALM code embebed in cs clases correctly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900