-3

I am trying to build a WPF desktop app and my HomeWindow is defined like this:

<Window x:Class="App1.HomeWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:App1"
    xmlns:MainWindow="clr-namespace:App1"
    xmlns:uc="clr-namespace:App1.UserControls.Home"
    mc:Ignorable="d" UseLayoutRounding="True"
    Title="App 1" Height= "750" Width="1200" MinHeight="500" MinWidth="800" WindowStyle="ThreeDBorderWindow" WindowStartupLocation="CenterScreen">

When the app is first loaded, I store the username and the client name in a simple class that is defined like this:

public static class LoggedInData
{
   public static string LoggedInUserName { get; set; }
   public static string LoggedClientName { get; set; }
}

What I would like to accomplish is, instead of just displaying a text in the title of the window, I would like to display the static text App1, the value of LoggedInUserName and the value of LoggedClientName.

I have tried some of the answers I have seen here but I have failed to reproduce them with my app. Can someone help me?

thatguy
  • 21,059
  • 6
  • 30
  • 40

1 Answers1

1

You can achive that with a multi-binding in the Title property of the window and string formatting. All you have to do is ensure that the static LoggedInUserName and LoggedClientName properties are assigned before the window is shown.

<Window x:Class="App1.HomeWindow" ...>
   <Window.Title>
      <MultiBinding StringFormat="{}App1 {0} {1}">
         <Binding Source="{x:Static local:LoggedInData.LoggedClientName}"/>
         <Binding Source="{x:Static local:LoggedInData.LoggedInUserName}"/>
      </MultiBinding>
   </Window.Title>
   <!-- ...your other code. -->
</Window>

You can adapt the StringFormat to fit your requirements. The {} prefix is just an escape sequence and {0} and {1} are placeholders for the bindings, the rest is up to you. Of course, you can also extend this to bind other properties on controls, the data context or view model.

thatguy
  • 21,059
  • 6
  • 30
  • 40
  • I have tried everything but I can't reproduce it. It's fair to mention that I am learning on my own and I have 0 background in programming. Can you show how the full code shoud be please? – Bruno Bukavu Thai Sep 04 '20 at 22:25