Accessing Controls in Code Behind inserted into Ribon Title Bar

Ribbon for WPF Forum

Posted 7 years ago by \アッカリーン/
Version: 12.2.0571
Avatar

Hi hi,

I had a question about inserting controls into the Ribon Title Bar. I seem to be unable to access the controls by name when added to my form's XAML code.

    <themes:WindowChrome.Chrome>
        <themes:WindowChrome IconMargin="10,4,10,4">
            <themes:WindowChrome.TitleBarContentTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBox Name="QuickSearchBox" Margin="5,2,5,0" Width="150" BorderBrush="Transparent" shared:HintProperties.Hint="Quick search..." KeyDown="QuickSearchBox_KeyDown" />
                    </StackPanel>
                </DataTemplate>
            </themes:WindowChrome.TitleBarContentTemplate>
        </themes:WindowChrome>
    </themes:WindowChrome.Chrome>

 I can't find a control named "QuickSearchBox" in the cs file for the codebehind, no what I try to change the name to, or how I try to manipulate the XAML. I've also tried "x:Name".  Any information on this would be extremely helpful, as I would like to control when focus is applied to that textbox programatically from within my code.

Thanks~

Comments (2)

Posted 7 years ago by \アッカリーン/
Avatar

You know, I just realized this is in a DataTemplate... I should probably read closer next time :(

Anyway... If anyone else runs into the same problem and needs a solution, the easiest way is probably just to add a "Loaded" event handler:

<TextBox Name="QuickSearchBox" Margin="5,2,5,0" 
Width="150" BorderBrush="Transparent" 
shared:HintProperties.Hint="Quick search..." 
KeyDown="QuickSearchBox_KeyDown" Loaded="QuickSearchBox_Loaded" />

 And then just save each sender to process later.

Answer - Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello, before we get into your question, you can't really apply what you have to a RibbonWindow if that is what you are doing.  RibbonWindow already has a WindowChrome instance in place and setting another on it will cause issues.  You can, however, use your code on any non-RibbonWindow.  Also RibbonWindow won't work too well with the TitleBarContent since things like contextual tabs appear can in that area.  So we don't recommend trying to add title bar content to RibbonWindow.  You could use Ribbon.TabPanelItems instead to put UI elements on the ribbon tab row.

If you did use the title bar content template, it's a DataTemplate meaning a ContentPresenter is presenting the TitleBarContentTemplate.  So as you saw, things like x:Name won't work.  But your workaround is a good way to handle that.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.