Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It looks like XamlReader put restrictions on the xaml it loads so it can not contain unknown element names not defined in the loaded xml. Instead use data binding to properties which works fine but requires some code behind. Below code will change the background on the loaded control when user changes the selected item of the comboboxes. You need to add a value converter that converts the colors to human readable names to be used by the comboboxes. </p> <p>XAML:</p> <p></p> <pre><code>&lt;StackPanel&gt; &lt;ComboBox ItemsSource="{Binding Path=AvailableColors}" SelectedValuePath="Color" SelectedValue="{Binding Path=SelectedColors[color0].Color}" /&gt; &lt;ComboBox ItemsSource="{Binding Path=AvailableColors}" SelectedValuePath="Color" SelectedValue="{Binding Path=SelectedColors[color1].Color}" /&gt; &lt;ContentControl Name="newControl" /&gt; &lt;/StackPanel&gt; </code></pre> <p></p> <p>Code behind:</p> <pre><code>using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Windows; using System.Windows.Markup; using System.Windows.Media; using System.Xml; namespace LoadTest { public partial class Window1 : Window { public Window1() { InitializeComponent(); SelectedColors = new Dictionary&lt;string, ColorInfo&gt;(); SelectedColors.Add("color0", AvailableColors.First()); SelectedColors.Add("color1", AvailableColors.Last()); XmlReader xaml = XmlReader.Create("newcontrol.xml"); newControl.Content = XamlReader.Load(xaml); newControl.DataContext = SelectedColors; DataContext = this; } public List&lt;ColorInfo&gt; AvailableColors { get { return new List&lt;ColorInfo&gt;() { new ColorInfo() {Color = Colors.Red }, new ColorInfo() { Color = Colors.Green }, new ColorInfo() { Color = Colors.Blue }, new ColorInfo() { Color = Colors.Yellow } }; } } public Dictionary&lt;string, ColorInfo&gt; SelectedColors { get; private set;} } public class ColorInfo : INotifyPropertyChanged { private Color _color; public Color Color { get { return _color; } set { _color = value; if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs("Color")); } } } public event PropertyChangedEventHandler PropertyChanged; } } </code></pre> <p>XAML file to load (newcontrol.xml):</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;Viewbox xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Name="Document" Stretch="Fill"&gt; &lt;Canvas Height="64" Width="128" ClipToBounds="True"&gt; &lt;Canvas.Background&gt; &lt;LinearGradientBrush StartPoint="1,0"&gt; &lt;GradientStop Color="{Binding Path=[color0].Color}" Offset="0" /&gt; &lt;GradientStop Color="{Binding Path=[color1].Color}" Offset="1" /&gt; &lt;/LinearGradientBrush&gt; &lt;/Canvas.Background&gt; &lt;/Canvas&gt; &lt;/Viewbox&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload