方法 : 背景イメージを使用して TextBox の操作性を高める
更新 : 2007 年 11 月
TextBox 内に説明の背景イメージを表示することによって TextBox の操作性を高める方法を次の例に示します。この背景イメージは、ユーザーがテキストを入力するまで表示され、入力が行われた時点で削除されます。また、背景イメージは、ユーザーが入力を削除すると、再び復元されます。次の図を参照してください。
メモ : |
---|
この例では、TextBox の Text プロパティを操作するのではなく、背景イメージが使用されています。それは、背景イメージがデータ バインディングに干渉しないためです。 |
使用例
<Page
xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
x:Class="SDKSample.TextBoxBackgroundExample"
>
<StackPanel>
<TextBox Name="myTextBox" TextChanged="OnTextBoxTextChanged" Width="200">
<TextBox.Background>
<ImageBrush ImageSource="TextBoxBackground.gif" AlignmentX="Left" Stretch="None" />
</TextBox.Background>
</TextBox>
</StackPanel>
</Page>
using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace SDKSample
{
public partial class TextBoxBackgroundExample : Page
{
void OnTextBoxTextChanged(object sender, TextChangedEventArgs e)
{
if (myTextBox.Text == "")
{
// Create an ImageBrush.
ImageBrush textImageBrush = new ImageBrush();
textImageBrush.ImageSource =
new BitmapImage(
new Uri(@"TextBoxBackground.gif", UriKind.Relative)
);
textImageBrush.AlignmentX = AlignmentX.Left;
textImageBrush.Stretch = Stretch.None;
// Use the brush to paint the button's background.
myTextBox.Background = textImageBrush;
}
else
{
myTextBox.Background = null;
}
}
}
}