-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up a WPF component for a text box with a placeholder.
- Loading branch information
1 parent
412c9ef
commit b56afdf
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
...ty/UniversalAssetTool/UniversalAssetTool.Ui/src/ui/wpf/common/TextBoxWithPlaceholder.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<UserControl x:Class="uni.src.ui.wpf.common.TextBoxWithPlaceholder" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:local="clr-namespace:uni.src.ui.wpf.common" | ||
mc:Ignorable="d" | ||
d:DesignHeight="24" | ||
d:DesignWidth="200"> | ||
<UserControl.DataContext> | ||
<local:TextBoxWithPlaceholderViewModel /> | ||
</UserControl.DataContext> | ||
|
||
<Grid> | ||
<TextBox x:Name="impl_" | ||
TextChanged="TextChanged_" /> | ||
<Label x:Name="placeholderLabel_" | ||
Content="{Binding Placeholder }" | ||
Background="Transparent" | ||
Opacity="0.5" | ||
IsHitTestVisible="False" /> | ||
</Grid> | ||
</UserControl> |
43 changes: 43 additions & 0 deletions
43
...UniversalAssetTool/UniversalAssetTool.Ui/src/ui/wpf/common/TextBoxWithPlaceholder.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.ComponentModel; | ||
using System.Windows.Controls; | ||
using System.Windows; | ||
|
||
namespace uni.src.ui.wpf.common { | ||
/// <summary> | ||
/// Interaction logic for WatermarkTextBox.xaml | ||
/// </summary> | ||
public partial class TextBoxWithPlaceholder : UserControl { | ||
private readonly TextBoxWithPlaceholderViewModel viewModel_; | ||
|
||
public TextBoxWithPlaceholder(TextBoxWithPlaceholderViewModel viewModel) { | ||
InitializeComponent(); | ||
this.viewModel_ = viewModel; | ||
} | ||
|
||
public string Placeholder { | ||
get => this.viewModel_.Placeholder; | ||
set => this.viewModel_.Placeholder = value; | ||
} | ||
|
||
private void TextChanged_(object sender, TextChangedEventArgs args) | ||
=> this.placeholderLabel_.Visibility | ||
= this.impl_.Text != "" | ||
? Visibility.Hidden | ||
: Visibility.Visible; | ||
} | ||
|
||
public class TextBoxWithPlaceholderViewModel : INotifyPropertyChanged { | ||
private string placeholder_ = "Search..."; | ||
public event PropertyChangedEventHandler? PropertyChanged; | ||
|
||
public string Placeholder { | ||
get => this.placeholder_; | ||
set { | ||
this.placeholder_ = value; | ||
this.PropertyChanged?.Invoke( | ||
this, | ||
new PropertyChangedEventArgs(nameof(Placeholder))); | ||
} | ||
} | ||
} | ||
} |