带有图像的 WPF 文本框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13927364/
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
WPF textbox with image
提问by 0070
I'm doing a WPF login interface. In my login panel, I have one login TextBoxand a PasswordBox. As what shown in the first image below, there is a little human logo in the login textbox and a lock in the password box. I set the image into the textbox background, and then when i try to insert some word into the login box, the words will overide the human logo(image B). Any advice to make it right?
我正在做一个WPF登录界面。在我的登录面板中,我有一个登录名TextBox和一个PasswordBox. 如下面的第一张图所示,登录文本框中有一个小人物徽标,密码框中有一个锁。我将图像设置为文本框背景,然后当我尝试在登录框中插入一些单词时,这些单词将覆盖人类徽标(图像 B)。有什么建议可以使它正确吗?
My XAML:
我的 XAML:
<TextBox Width="380" Height="25" HorizontalAlignment="Center" Foreground="WhiteSmoke" BorderBrush="Transparent" >
<TextBox.Background>
<ImageBrush ImageSource="/icon/user_login.png" AlignmentX="Left" Stretch="None"></ImageBrush>
</TextBox.Background>
</TextBox>
Image A:
图一:


Image B:
图 B:


回答by JosephGarrone
My suggestion is that you replace each of the Textbox'swith a DockPanel. In which they each have an Imageas the left-most item and a Textboxas the right most. Then set the images to User and Lock respectively. Then set the backgrounds of the Textboxand Imagesto transparent. You can then set whatever styling you want on the DockPanel.
我的建议是你Textbox's用一个DockPanel. 其中他们每个人都有一个Image作为最左边的项目和一个Textbox作为最右边的项目。然后将图像分别设置为用户和锁定。然后设置的背景的Textbox和Images为透明。然后,您可以在DockPanel.
EDIT 1 - Copy paste from working example
编辑 1 - 从工作示例复制粘贴
Code:
代码:
<DockPanel>
<Button BorderThickness="0" DockPanel.Dock="Left" HorizontalAlignment="Right" Height="28" Width="23">
<DynamicResource ResourceKey="SearchBar"/>
</Button> 'This is a button, because I have a custom Style which I am using which makes all the borders go away. And also because I use it to clear the field.
<TextBox Text="Search..." FontSize="16" HorizontalAlignment="Stretch" Background="Transparent"/>
</DockPanel>
Image:
图片:


By not setting the DockPanel.Dockproperty on the second item, I am telling it to stretch across the rest of the DockPanel. Any other queries, please let me know. If you copy paste the above, it might not look the same, due to me cutting out irrelevant parts.
通过不在DockPanel.Dock第二个项目上设置属性,我告诉它延伸到DockPanel. 任何其他疑问,请告诉我。如果你复制粘贴上面的内容,它可能看起来不一样,因为我删掉了不相关的部分。
回答by Hasansalih
<DockPanel Grid.Row="1" Margin="65,24,71,11" HorizontalAlignment="Stretch">
<Image Source="/SDPI;component/Image/Profile.png"/>
<toolkit:WatermarkTextBox Grid.Row="1" FontSize="16">
<toolkit:WatermarkTextBox.Watermark>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Username" FontSize="15" Margin="4,0,0,0" />
</StackPanel>
</toolkit:WatermarkTextBox.Watermark>
</toolkit:WatermarkTextBox>
</DockPanel>

