---
lang: ja-jp
breaks: true
---
# WPF TextBox に `PlaceholderText`/`Cue Banner`/`Hint` を設定する 2022-08-29
## ModernWpfUI
> ModernWpf/test/TestAppUtils/Controls/TextBoxEx.cs
> https://github.com/Kinnara/ModernWpf/blob/8204dd15a72b22b3a584001cd0bbce03c6737e8d/test/TestAppUtils/Controls/TextBoxEx.cs#L35
> https://github.com/Kinnara/ModernWpf/blob/8204dd15a72b22b3a584001cd0bbce03c6737e8d/ModernWpf/Styles/TextBox.xaml#L151
### csproj
```xml=
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ModernWpfUI" Version="0.9.7-preview.2" />
</ItemGroup>
</Project>
```
### App.xaml
```xaml=
<Application x:Class="WPF_TextBox.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF_TextBox"
StartupUri="MainWindow.xaml"
xmlns:ui="http://schemas.modernwpf.com/2019"
>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ui:XamlControlsResources />
<ui:ThemeResources RequestedTheme="Light" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
```
### MainWindow.xaml
```xaml=
<Window x:Class="WPF_TextBox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF_TextBox"
xmlns:ui="http://schemas.modernwpf.com/2019"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
FontFamily="Meiryo UI" FontSize="24"
>
<Grid>
<StackPanel>
<TextBox></TextBox>
<TextBox
ui:ControlHelper.Header="ヘッダ文字列"
ui:ControlHelper.PlaceholderText="検索文字列"
>
</TextBox>
</StackPanel>
</Grid>
</Window>
```

## MahApps.Metro
### csproj
```xml=
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MahApps.Metro" Version="2.4.9" />
</ItemGroup>
</Project>
```
### App.xaml
```xaml=
<Application x:Class="WPF_MahApps_TextBox.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF_MahApps_TextBox"
StartupUri="MainWindow.xaml"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<!-- MahApps.Metro resource dictionaries. Make sure that all file names are Case Sensitive! -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
<!-- Theme setting -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Themes/Light.Blue.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
```
### MainWindow.xaml
```xaml=
<Window x:Class="WPF_MahApps_TextBox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF_MahApps_TextBox"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
FontFamily="Meiryo UI" FontSize="24"
>
<Grid>
<StackPanel>
<TextBox mah:TextBoxHelper.Watermark="検索文字列" />
<TextBox mah:TextBoxHelper.Watermark="検索文字列" mah:TextBoxHelper.ClearTextButton="True" />
</StackPanel>
</Grid>
</Window>
```

## Material Design In XAML Toolkit
### csproj
```xml=
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MaterialDesignThemes" Version="4.6.0-ci277" />
</ItemGroup>
</Project>
```
### App.xaml
```xaml=
<Application x:Class="WPF_MaterialDesignThemes_TextBox.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF_MaterialDesignThemes_TextBox"
StartupUri="MainWindow.xaml"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
>
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<materialDesign:BundledTheme BaseTheme="Light" PrimaryColor="DeepPurple" SecondaryColor="Lime" />
<ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
```
### MainWindow.xaml
```xaml=
<Window x:Class="WPF_MaterialDesignThemes_TextBox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF_MaterialDesignThemes_TextBox"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800"
FontFamily="Meiryo UI" FontSize="24"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
>
<Grid>
<StackPanel>
<TextBox></TextBox>
<TextBox
materialDesign:HintAssist.Hint="検索文字列"
/>
<TextBox
Style="{StaticResource MaterialDesignFilledTextBox}"
materialDesign:HintAssist.Hint="検索文字列"
/>
<TextBox
Style="{StaticResource MaterialDesignFloatingHintTextBox}"
materialDesign:HintAssist.Hint="検索文字列"
/>
</StackPanel>
</Grid>
</Window>
```




## その他
> WPFでTextBoxにPlaceHolderを表示する
> https://www.id-frontier.jp/blog/tech/wpf%E3%81%A7textbox%E3%81%ABplaceholder%E3%82%92%E8%A1%A8%E7%A4%BA%E3%81%99%E3%82%8B/
###### tags: `WPF` `TextBox` `PlaceholderText` `cue banner` `Hint` `ModernWpfUI` `MahApps.Metro` `MaterialDesignThemes`