Change a Xaml root property, i.e. Title, in code behind

David Hoffman 121 Reputation points
2021-09-29T18:46:04.173+00:00

I want to change the Title root property in the following XAML file in code behind. I have tried several ideas with no success.

<ContentDialog
    x:Class="KCam8_GUI_Test.LogInContentDialog"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="KCam8_GUI_Test"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    Title="USER SIGN IN"
    PrimaryButtonText="Sign In"  
    SecondaryButtonText="Clear" 
    CloseButtonText="Cancel"
    PrimaryButtonClick="ContentDialog_PrimaryButtonClick"
    SecondaryButtonClick="ContentDialog_SecondaryButtonClick"
    CloseButtonClick="ContentDialog_CloseButtonClick">

    <ContentDialog.Resources>
        <!-- These flyouts or used for confirmation when the user changes
         the option to save their user name. -->
    </ContentDialog.Resources>

    <StackPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
        <TextBox x:Name="UserName" Header="User name"/>
        <PasswordBox x:Name="Password" Header="Password" Margin="0,0,0,10" PasswordRevealMode="Peek"/>
        <TextBlock x:Name="ErrorText" Foreground="Red" Text="Place holder" Margin="0,0,0,10" Visibility="Collapsed"/>
        <TextBlock x:Name="Instructions" TextWrapping="Wrap">
            <TextBlock.Text>
                Sign in with your User name and/or Password.
            </TextBlock.Text>
        </TextBlock>
    </StackPanel>
</ContentDialog>

My code behind would be something like:

<root>.Title(L"NEW TITLE");

I just can't figure out what goes in place of <root>. In this case I think it should be ContentDialog(). But if I do use this, then the Title(L"NEW TITLE") gives an error It also gives an error if I set Title() = L"NEW TITLE"; I even remove the L, but it does not help. Not sure what the answer is. Thanks for your help.

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Roy Li - MSFT 32,051 Reputation points Microsoft Vendor
    2021-09-30T06:13:30.94+00:00

    Hello,

    Welcome to Microsoft Q&A!

    What about calling it like this:

    Title(box_value(L"Clicked"));  
    

    Or maybe you could call it before you show the ContentDialog like:

    dialog.Title(box_value(L"title"));  
    

    Thank you.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Michael Taylor 48,581 Reputation points
    2021-09-29T19:06:42.507+00:00

    The Title property is on the ContentDialog instance. If you're doing this in the codebehind then you don't need anything before the Title. Just assign it a value. If this is anywhere else then you need to get the ContentDialog instance that you are changing the title of.

    Note that in UWP the solution to everything is binding to a property on your view model. So ideally just do that and forget the code behind altogether.

    Your code looks like C++ with the L"" syntax. Are you doing this via C++, C# or what? Can you post the actual code block that you're trying to get working? If you're using C++ (which doesn't have properties) then you use the corresponding function name.

    //In the ContentDialog code behind
    Title(L"Some title");
    
    1 person found this answer helpful.