【问题标题】:how to set image in textbox in wpf如何在wpf的文本框中设置图像
【发布时间】:2020-04-08 02:59:22
【问题描述】:

我的应用程序中有一个文本框,我想向其中添加图像。

在某些情况下,我想要图片和文字。

有可能吗?如何做到这一点?

谢谢。

【问题讨论】:

    标签: wpf


    【解决方案1】:

    来自 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

    【讨论】:

    • 是否也可以偏移文本使其不覆盖图标?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-28
    相关资源
    最近更新 更多