Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding items to a menu in a usercontrol
    primarykey
    data
    text
    <p>I've created a user control with two items: a button and a menu. The code below is a horribly simplified version that still has the same problems (which I will mention below).</p> <pre><code>&lt;UserControl x:Class="gemsTouchLensApplication.Controls.ButtonAndMenu" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;Button/&gt; &lt;Menu/&gt; &lt;/StackPanel&gt; &lt;/UserControl&gt; </code></pre> <p>I want to be able to do two things with this usercontrol: add items to the menu and set the content of the button. </p> <p>I would like to be able to add items to the menu the conventional way</p> <pre><code>&lt;Menu&gt; &lt;MenuItem Header="1"/&gt; &lt;MenuItem Header="2"/&gt; &lt;MenuItem Header="3"/&gt; &lt;MenuItem Header="4"/&gt; &lt;MenuItem Header="5"/&gt; etc... &lt;/Menu&gt; </code></pre> <p>However, when I try to do this after adding my usercontrol to a stackpanel </p> <pre><code>&lt;Window&gt; &lt;StackPanel&gt; &lt;cntrls:ButtonAndMenu&gt; &lt;MenuItem Header="1"/&gt; &lt;MenuItem Header="2"/&gt; &lt;!--Comment this out and the error disappears--&gt; &lt;/cntrls:ButtonAndMenu&gt; &lt;/StackPanel&gt; &lt;/Window&gt; </code></pre> <p>something unexpected happens. I can only add a single item to the usercontrol. If I add more than one item I get the error:</p> <p>Property 'Content' can only be set once.</p> <p>When only a single item is added the controls visual is closer to that of a button, not a menu.</p> <p>I suspect that by adding controls to the usercontrol the WPF engine defaults to setting the content of the button instead of adding the controls to the menu.</p> <p>How can I modify the usercontrol so that the consumer can: </p> <ol> <li>Add items to the menu using conventional notation </li> <li>Still set the content of the button</li> </ol>
    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