Dynamic Management Pattern in Silverlight

I am trying to make a button in Silverlight using a control template to change the way it is displayed.

I need to do this dynamically in code (and not xaml markup). The Button object has a Template property to which you can assign a ControlTemplate.

But how do you type user interface elements in a ControlTemplate? (There is a VisualTree property in WPF, but no such property in Silverlight)

+1
source share
1 answer

I'm not sure if this helps, but just in case. To create buttons using the control pattern in the code behind (and not XAML), I did it like this:

  • download the control template from the xml definition (below is the link to the source)

    byte[] bytes = ReadBytesFromStream("BestBuyRemix.BL.buttontemplate.xml"); string buttonTemplate = ""; UTF8Encoding encoding = new UTF8Encoding(); buttonTemplate = encoding.GetString(bytes.ToArray(), 0, (int)bytes.Length); 
  • create a button and add it to the visual tree (in this case, the wrapper panel)

string onebutton = string.Format (buttonTemplate, mnu.CatItemName, mnu.CatItemImage, "{StaticResource buttonStyle1}", "{StaticResource CatItemNameBlock}", "{StaticResource ThumbNailPreview}", ictr.ToString; ictr + = 1;

  Button bt = (Button)XamlReader.Load(onebutton); bt.Tag = mnu.CatItemPageUri; bt.Click += new RoutedEventHandler(bt_Click); Wrappable.Children.Add(bt); 

I wrote a blog post about the Best Buy Remix API, which uses this to create a list of products on the details page. It has a link to a Silverlight source. If you are interested.
blog link

+2
source

Source: https://habr.com/ru/post/887602/


All Articles