1

I have a custom context menu on my window like so, which contains two color pickers--one for foreground and one for background:

<ContextMenu>
    <ContextMenu.Template>
        <ControlTemplate>
            <Border BorderBrush="Black" BorderThickness="1" Background="White">
                <UniformGrid Rows="2">
                    <Border BorderBrush="Black" BorderThickness="0,0,0,1">
                        <DockPanel Margin="5">
                            <Label Content="Background Color" Margin="0,0,10,0" DockPanel.Dock="Left"/>
                            <xctk:ColorPicker DisplayColorAndName="True" ColorMode="ColorCanvas" SelectedColor="Gray" DockPanel.Dock="Right"/>
                        </DockPanel>
                    </Border>
                    <DockPanel Margin="5">
                        <Label Content="Foreground Color" Margin="0,0,10,0" DockPanel.Dock="Left"/>
                        <xctk:ColorPicker Name="cpForegroundColor" DisplayColorAndName="True" ColorMode="ColorCanvas" SelectedColor="White" DockPanel.Dock="Right"/>
                    </DockPanel>
                </UniformGrid>
            </Border>
        </ControlTemplate>
    </ContextMenu.Template>
</ContextMenu>

How exactly could I bind the colorpicker's SelectedColor to a label I have inside the window?

I've attempted using ElementName but I guess this won't work due to differing namescopes. From what I've read it may not be a good idea to bind to things inside a control template as well.

Any suggestions on how (or a better way) to do this would be greatly appreciated--thanks!

cjones26
  • 3,459
  • 1
  • 34
  • 51
  • 1
    Do either of these help? http://stackoverflow.com/questions/1013558/elementname-binding-from-menuitem-in-contextmenu, http://stackoverflow.com/questions/3668654/relativesource-binding-from-a-tooltip-or-contextmenu – Scott Jan 09 '15 at 00:02
  • Sort of pointing me in the right direction but I think the main issue lies with the fact that I'm using the custom control template. – cjones26 Jan 09 '15 at 14:17
  • They definitely pointed me in the right direction! I was able to remediate the issue by creating a view model, set the tag on my border (which houses the context menu) like shown in one of the answers, then bind to my view model's property via the PlacementTarget. Thank you! – cjones26 Jan 09 '15 at 14:39
  • 1
    Cool, seems to be the consensus that you have to do a bit of trickery when there are bindings into or out of the scope of a popup/context menu. Pleased you got it sorted. – Scott Jan 11 '15 at 21:43

0 Answers0