WPF 中带有图像图标的文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38304387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TextBox with image icon in WPF
提问by Neelam Prajapati
I want to create TextBoxwith image in it. This is what I have tried:
我想在其中创建TextBox图像。这是我尝试过的:
<DockPanel Grid.Row="1" Grid.Column="1" Margin="5" >
<Image DockPanel.Dock="Left" Source="D:\my_backup\WPF\SALIENT\SALIENT\Images\d2.PNG" Width="20" Height="20"></Image>
<TextBox Text="test" FontSize="16" HorizontalAlignment="Stretch" Background="Transparent"
</TextBox>
</DockPanel>
this gives me output like this: 
but i want the image inside TextBoxlike this
anyone can help?
任何人都可以帮忙吗?
回答by MichaelThePotato
You could use this sort of implementation. you should probably make a user control out of it.
您可以使用这种实现方式。你可能应该让用户控制它。
<Border BorderBrush="Black"
BorderThickness="2"
VerticalAlignment="Center"
CornerRadius="5">
<StackPanel Margin="5"
Orientation="Horizontal">
<Image Source="C:\SourceOfTheImage\Path\Image.png"
Height="18"/>
<TextBlock Text="Hello, I am a text block!"
Margin="3 0 0 0"/>
</StackPanel>
</Border>
It looks like this for me
对我来说看起来像这样
回答by A.Pissicat
You can set the background property on Textbox, like this (mine is align on right) :
您可以在 Textbox 上设置背景属性,如下所示(我的在右侧对齐):
<TextBox x:Name="txtSearch"
Text="Search Item...">
<TextBox.Background>
<ImageBrush ImageSource="Images/Search.png" Stretch="Uniform" AlignmentX="Right">
<ImageBrush.Transform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform/>
<TranslateTransform X="-3"/>
</TransformGroup>
</ImageBrush.Transform>
</ImageBrush>
</TextBox.Background>
</TextBox>
Set AlignmentXto left if you want to see the image on the left side. Set the TranslateTransform.Xto a positive value to add a margin.
AlignmentX如果您想在左侧看到图像,请设置为左侧。将 设置TranslateTransform.X为正值以添加边距。
回答by Logan
Try this:
尝试这个:
<Border Padding="5" BorderThickness="2,2,2,2" BorderBrush="Gray" CornerRadius="2,2,2,2">
<DockPanel Grid.Row="1" Grid.Column="1" Margin="5" >
<Image DockPanel.Dock="Left" Source="D:\my_backup\WPF\SALIENT\SALIENT\Images\d2.PNG" Width="20" Height="20"></Image>
<TextBox Text="test" FontSize="16" HorizontalAlignment="Stretch" Background="Transparent" BorderBrush="Transparent" ></TextBox>
</DockPanel>
</Border>
That would be the simplest one-off way of doing it. You could dump it in a UserControl for reuse.
这将是最简单的一次性方式。您可以将其转储到 UserControl 中以供重用。
A second way of achieving this would be to open up the TextBox template and put this icon of yours inside the makeup of the TextBox, which would allow you to avoid needing the DockPanel and Border here, as well as allowing you to make the Template a resource you can easily attach to any Textbox in the future.
实现此目的的第二种方法是打开 TextBox 模板并将您的这个图标放在 TextBox 的构成中,这将允许您避免在此处使用 DockPanel 和 Border,并允许您将模板制作为您将来可以轻松附加到任何文本框的资源。

