Nasıl yapılır: TextBox'a Filigran Ekleme

Aşağıdaki örnek, TextBoxTextBox Kullanıcı metin girişi yapılıncaya kadar ' ın içinde açıklayıcı bir arka plan görüntüsü görüntüleyerek bir öğesinin kullanılabilirliğine nasıl yardımcı olduğunu gösterir. Ayrıca, Kullanıcı girişlerini kaldırırsa arka plan görüntüsü yeniden geri yüklenir. Aşağıdaki çizime bakın.

A TextBox with a background image

Not

Bu örnekte, bir arka plan resminin, özelliğini işlemek yerine kullanıldığı neden, TextTextBox arka plan resminin veri bağlamayı etkilememesini sağlamaktır.

Örnek

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://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;
            }
        }
    }
}

Namespace SDKSample
    Partial Public Class TextBoxBackgroundExample
        Inherits Page

        Private Sub OnTextBoxTextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)

            If myTextBox.Text = "" Then

                ' Create an ImageBrush.
                Dim textImageBrush As 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 = Nothing
            End If

        End Sub

    End Class

End Namespace

Ayrıca bkz.