Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF Controls as StaticResource in Resource Dictionary, used in multiple WPF Windows?
    primarykey
    data
    text
    <p>I have a Button control as a resource in Resource Dictionary as below:</p> <pre><code>&lt;!--ButtonResources.xaml file--&gt; &lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt; &lt;Button x:Key="buttonResource" Content={Binding BoundText}/&gt; &lt;/ResourceDictionary&gt; &lt;!--ButtonResources.xaml file--&gt; </code></pre> <p>I now use this above button control <strong>bound to Content property of ContentControl controls</strong> in <strong>2 different Windows</strong> .xaml files where each <code>Window</code> has its own <code>DataContext</code> and thus each window should display the <code>Content</code> of above button control based on its <code>ViewModel's</code> <code>BoundText</code> property value as below for each Window.</p> <pre><code>&lt;Window x:Class="TestClass1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt; &lt;Window.Resources&gt; &lt;ResourceDictionary&gt; &lt;ResourceDictionary.MergedDictionaries&gt; &lt;ResourceDictionary Source="ButtonResources.xaml"/&gt; &lt;/ResourceDictionary.MergedDictionaries&gt; &lt;/ResourceDictionary&gt; &lt;/Window.Resources&gt; &lt;Grid&gt; &lt;ContentControl Content={StaticResource buttonResource}/&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>But, the problem is that both Window's display same value for the BoundText property which means that both the WPF Windows have <strong><em>same</em></strong> instance of button control from resource, used in both the Windows.</p> <p>How can I resolve this problem such that each Window gets a <em>separate</em> button control from the resource and still display <strong><em>different</em></strong> values for the <code>BoundText</code> property from their <strong>own ViewModel</strong>?</p> <p><strong>Edit:</strong> For the reason mentioned in <code>MSDN</code> as below, I can't use x:Shared="False" attribute to resolve this:</p> <blockquote> <p>•The ResourceDictionary that contains the items must not be nested within another ResourceDictionary. For example, you cannot use x:Shared for items in a ResourceDictionary that is within a Style that is already a ResourceDictionary item.</p> </blockquote>
    singulars
    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