如何在wpf的文本框中设置图像

时间:2011-11-09 20:07:25

标签: wpf

我在我的应用程序中有TextBox,我想为此添加图像,有时候

与Microsoft Office Word一样,我将同时拥有文本和图像

可能吗?

如果有可能怎么样?

感谢。

1 个答案:

答案 0 :(得分:5)

来自XAML:

<TextBox Name="myTextBox" TextChanged="OnTextBoxTextChanged" Width="200">
  <TextBox.Background>
    <ImageBrush ImageSource="TextBoxBackground.gif" AlignmentX="Left" Stretch="None" />
  </TextBox.Background>
</TextBox>

来自C#

// 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;

Source