Note that there are some explanatory texts on larger screens.

plurals
  1. POWPF: Error using a custom control in the ItemTemplate of another custom control
    primarykey
    data
    text
    <p>I have a custom control library where I have several custom controls that I use in my main application. I have a simple custom control that lets the user select pen thickness values from a combobox. Now, I am creating a new custom control, based on a listbox, and I want to include the pen thickness combobox in the new custom control's ItemTemplate.</p> <p>I am getting this error:</p> <p><strong>"Cannot create instance of "LineThicknessComboBox" defined in assembly CustomControls ... Exception has been thrown by the target of an invocation. Error at object "10_T" in markup file 'CustomControls;component/Themes/CustomListBox.General.xaml'.</strong></p> <p>Here is the XAML and code behind for the LineThicknessComboBox:</p> <pre><code>namespace CustomControls { public class LineThicknessComboBox : ComboBox { public LineThicknessComboBox() { DefaultStyleKeyProperty.OverrideMetadata(typeof(LineThicknessComboBox) , new FrameworkPropertyMetadata(typeof(LineThicknessComboBox))); } } } </code></pre> <p>and in LineThicknessCombobox.Generic.xaml:</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:CustomControls"&gt; &lt;Style TargetType="{x:Type local:LineThicknessComboBox}" BasedOn="{StaticResource {x:Type ComboBox}}"&gt; &lt;Setter Property="ItemTemplate"&gt; &lt;Setter.Value&gt; &lt;DataTemplate&gt; ... &lt;/DataTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/ResourceDictionary&gt; </code></pre> <p>Here is the XAML and codebehind for my new CustomListBox:</p> <pre><code>namespace CustomControls { public class CustomListBox : ListBox { public CustomListBox() { DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomListBox) , new FrameworkPropertyMetadata(typeof(CustomListBox))); } } } </code></pre> <p>and in CustomListBox.Generic.xaml:</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:CustomControls"&gt; &lt;Style TargetType="{x:Type local:CustomListBox}" BasedOn="{StaticResource {x:Type ListBox}}"&gt; &lt;Setter Property="ItemTemplate"&gt; &lt;Setter.Value&gt; &lt;DataTemplate&gt; &lt;Border&gt; ... &lt;local:LineThicknessComboBox /&gt; ... &lt;/Border&gt; &lt;/DataTemplate&gt; &lt;/Setter.Value&gt; &lt;/Setter&gt; &lt;/Style&gt; &lt;/ResourceDictionary&gt; </code></pre> <p>And here is my Generix.xaml:</p> <pre><code>&lt;ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt; &lt;ResourceDictionary.MergedDictionaries&gt; &lt;ResourceDictionary Source="CustomControls;component/Themes/LineThicknessComboBox.Generic.xaml"/&gt; &lt;ResourceDictionary Source="CustomControls;component/Themes/CustomListBox.Generic.xaml"/&gt; &lt;/ResourceDictionary.MergedDictionaries&gt; &lt;/ResourceDictionary&gt; </code></pre> <p>I'm thinking I have some kind of reference problem, but can't figure out what is wrong. The program compiles and runs without any warnings/errors but when the CustomListBox is created in my main application, I get the error listed above. Without including the LineThicknessComboBox, the CustomListBox works fine.</p> <p>Can anyone explain why I am getting this error? It is possible to include my custom control in another, even though they are in the same custom control library, correct?</p> <p>Thanks!</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