Note that there are some explanatory texts on larger screens.

plurals
  1. POLoaded XAML Not Binding to Existing Elements Correctly
    primarykey
    data
    text
    <p>I have a button that loads XAML from a file, creates a control from it and adds this control as a child to a canvas that is part of a template present in the resources of a dockpanel on the window. The window also has a combobox named cboTColour and a combobox named cboBColour which I use to set a simple gradient background on my loaded control.</p> <p>I load the XAML and add it to my canvas using the following code:</p> <pre><code>XmlReader xaml = XmlReader.Create(filename); newControl = (Viewbox)XamlReader.Load(xaml); ((Canvas)(testButton.Template.FindName("MyCanvas", testButton))).Children.Clear(); ((Canvas)(testButton.Template.FindName("MyCanvas", testButton))).Children.Add(newControl); </code></pre> <p>And here is the XAML I load:</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;!--Horizontal Gradient--&gt; &lt;LinearGradientBrush StartPoint="1,0"&gt; &lt;GradientStop Color="{Binding ElementName=cboTColour, Path=SelectedItem.Name}" Offset="0"&gt;&lt;/GradientStop&gt; &lt;GradientStop Color="{Binding ElementName=cboBColour, Path=SelectedItem.Name}" Offset="1"&gt;&lt;/GradientStop&gt; &lt;/LinearGradientBrush&gt; &lt;/Canvas.Background&gt; &lt;/Canvas&gt; &lt;/Viewbox&gt; </code></pre> <p>I have tried putting the XAML straight into the designer and it works perfectly so it is not an issue with that. When I load the XAML from file, the controls are being created and placed correctly, but the databinding does not work - the colours do not change. I get the following errors:</p> <pre><code>System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=cboTColour'. BindingExpression:Path=SelectedItem.Name; DataItem=null; target element is 'GradientStop' (HashCode=24393646); target property is 'Color' (type 'Color') System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=cboBColour'. BindingExpression:Path=SelectedItem.Name; DataItem=null; target element is 'GradientStop' (HashCode=23972246); target property is 'Color' (type 'Color') </code></pre> <p>I assume what's happening is when the XAMLReader loads the xaml ad creates a control from it, it is not sure of the path to my combo-boxes, as the xaml is not yet part of the window, and when the control is added to the window, this binding doesn't update, but I do not know how to either modify my bindings in the XAML to reflect where my combo-boxes would be situated in relation to it or to modify the XAMLReader or overall datacontext to take into account my controls. I can also assure you that the comboboxes have been created by this point as the code runs when a button is pressed on the window the comboboxes are in.</p> <p>I MUST specify that I CANNOT modify the bindings themselves in the code, as the bindings will appear in various places and at various times throughout the different XAML files I will load.</p> <p>Any help would be much appreciated.</p>
    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. 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