Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does Style not work proper on Popup in WPF?
    text
    copied!<p>I am not sure if i do not use the correct way, so I try to describe my case here. Please help to point out the detail.</p> <p>The purpose of mine is to customize the WPF <code>Popup</code> class and implement a specific popup panel. I use the CustomControl to implement this as below:</p> <p>Here is the <code>Generic.xaml</code> </p> <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication3"&gt; &lt;Style TargetType="{x:Type local:CCPopup}"&gt; &lt;Setter Property="AllowsTransparency" Value="True" /&gt; &lt;Setter Property="StaysOpen" Value="False" /&gt; &lt;Setter Property="Placement" Value="Center" /&gt; &lt;Setter Property="Child"&gt; &lt;Setter.Value&gt; &lt;Border Background="Red" Width="30" Height="30" /&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/ResourceDictionary&gt; </code></pre> <p>and the following is the code, which only register the meta style information:</p> <pre><code>public class CCPopup : Popup { static CCPopup() { DefaultStyleKeyProperty.OverrideMetadata(typeof(CCPopup), new FrameworkPropertyMetadata(typeof(CCPopup))); } } </code></pre> <p>The problems I met until now can be summarized as below:</p> <p>1) I try to write:</p> <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication3"&gt; &lt;Style TargetType="{x:Type local:CCPopup}" BasedOn="{StaticResource {x:Type Popup}}"&gt; &lt;Setter Property="AllowsTransparency" Value="True"/&gt; &lt;Setter Property="StaysOpen" Value="False" /&gt; &lt;Setter Property="Placement" Value="Center" /&gt; &lt;Setter Property="Child"&gt; &lt;Setter.Value&gt; &lt;Border Background="Red" Width="30" Height="30" /&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/ResourceDictionary&gt; </code></pre> <p>But this does not works, and I got the error <code>{x:Type Popup}</code> could not be resolved. Why?</p> <p>About my MainWindow.xaml: I only add <code>&lt;CCPopup x:Key="popup" /&gt;</code> in its resource and create a button, when I click the button, I bind the popup's <code>PlacementTarget</code> to this button and set popup's <code>IsOpen</code> property to true in code. </p> <p>2) When I create a instance of CCPopup in the code, such as <code>CCPopup p = new CCPopup()</code>, some of the Style does not works, such as <code>AllowsTransparency</code> and <code>StaysOpen</code>, will not be set to the value I give in the xaml Style. But when I create a instance from xaml <code>&lt;CCPopup x:Key="popup" /&gt;</code> and refer to it from resource in code, the style works correctly. Can you point out where I made a mistake ?</p> <p>3) Just curious about why <code>Popup</code> class do not have <code>Template</code> property?</p> <p>Thanks for your help.</p>
 

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