Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble binding a checkable MenuItem to parent Window's Topmost (always on top) property
    primarykey
    data
    text
    <p>I can't figure out where I'm going wrong here. Hopefully one of you can help.</p> <p>I have a Window that contains a TabControl. The TabControl has a ContextMenu with a checkable item for "always-on-top" behavior. I want to bind this checkable item to the containing Window's boolean Topmost property.</p> <p>No matter what I do, the binding fails and I get a "Cannot find source for binding" error in my debug output.</p> <p><em>(These excerpts are greatly simplified from my actual code. I hope I didn't accidentally cut out anything relevant.)</em></p> <p><strong>First I tried this:</strong></p> <pre><code>&lt;Window x:Name="myWindow" (blah blah other properties)&gt; &lt;TabControl x:Name="tabControl"&gt; &lt;TabControl.ContextMenu&gt; &lt;ContextMenu&gt; &lt;MenuItem Header="Always on Top" IsCheckable="True" IsChecked="{Binding ElementName=myWindow, Path=Topmost, Mode=TwoWay}"/&gt; &lt;/ContextMenu&gt; ... </code></pre> <p>That didn't work. Is it because the MenuItem is inside of the Window "myWindow"? Do I need to use a RelativeSource Ancestor binding?</p> <p><strong>So I tried this:</strong></p> <pre><code>&lt;Window x:Name="myWindow" (blah blah other properties)&gt; &lt;TabControl x:Name="tabControl"&gt; &lt;TabControl.ContextMenu&gt; &lt;ContextMenu&gt; &lt;MenuItem Header="Always on Top" IsCheckable="True" IsChecked="{Binding RelativeSource={ RelativeSource FindAncestor, AncestorType={x:Type Window} }, Path=Topmost, Mode=TwoWay}"/&gt; &lt;/ContextMenu&gt; ... </code></pre> <p>That didn't work either.</p> <p>So now I'm stuck. How do I make this binding work?</p> <p>Note: My code-behind is not doing anything with these elements. Do I need to set Window.DataContext or something? That could possibly break other parts of this window.</p>
    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