0

Is there a way to convert string to UIElement? I'm trying to add text to my graph's legend using an Interactive data display. The legend needs to not correlate with a plotted graph so I'm making my own like so:

LegendItemsPanel legendItemsPanel = new LegendItemsPanel() 
{
            
};
Legend legend = new Legend() 
{
   Content = legendItemsPanel
};
Rectangle rectUP = new Rectangle()
{
   Width = 10,
   Height = 10,
   Fill = new SolidColorBrush(Colors.Red),
   Stroke = new SolidColorBrush (Colors.Red),
};
UIElement element() 
{
   UIElement output = new UIElement();
   output.Equals(rectUP + " Upstream band");
   return output;
}
legendItemsPanel.Children.Add(element());

I've tried a lot of possibilities but every time I get the same error "Cannot convert from 'string' to 'Windows.System.UIElement'". Could someone please help me out with this one? Thank you

TBA
  • 1,921
  • 4
  • 13
  • 26
David Hala
  • 13
  • 4
  • 1
    What is this line supposed to do: `output.Equals(rectUP + " Upstream band")`? – Klaus Gütter Dec 18 '21 at 11:41
  • I'm trying to bind rectangle and string together somehow. So i wanted to bind them into element() – David Hala Dec 18 '21 at 11:46
  • Creating a plain UIElement makes no sense. You probably want to create a more derived element, e.g. a TextBlock, and set its Text property. – Clemens Dec 18 '21 at 11:49
  • You can't add a string to a Rectangle, and Equals is not an assignment operator. See what @Clemens said. – Crowcoder Dec 18 '21 at 11:56
  • Yes, it was what i was looking for. Thank you @Clemens! May i ask one following question? How do i bind that textbox and rectangle so i can add them both as one? – David Hala Dec 18 '21 at 11:58
  • Put them into a common container element, i.e. a Panel. StackPanel would be the most simple one. Please also consider reading some introductory material, e.g. a book or online tutorial about the basics of WPF. – Clemens Dec 18 '21 at 12:03

0 Answers0