TextBox with image icon in WPF - wpf

I want to create TextBox with image in it. This is what I have tried:
<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 TextBox like this
anyone can help?

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

You can set the background property on Textbox, like this (mine is align on right) :
<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 AlignmentX to left if you want to see the image on the left side. Set the TranslateTransform.X to a positive value to add a margin.

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.
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.

Related

How to Create a Bordered Area in WPF That Has a Background Image and Changeable Text

I've been learning some coding and have received some great pointers here, so I thought I'd ask about my current endeavor.
I'm currently experimenting with a WPF app and would like to try to create something like this (pardon the color scheme... I just threw this together):
The desired outcome is:
Have a bordered area
The bordered area needs a background image
Programmatically changeable progress bar
Static text in various areas
Programmatically changeable text in various areas
I have no idea where to even begin on creating something like this. I'm thinking of it from the perspective of a graphics editor like Paint.Net where I'd create layers for each element. This may be the wrong way of thinking, but that's where I'm at.
Please remember I'm a novice, so you'll want to provide the "patiently explaining to a child who suffered head trauma" explanation.
Thanks!
Hello I have made a small sample.
Here are the XAML codes:
<StackPanel Orientation="Horizontal">
<Border BorderBrush="#FF3CFF00" BorderThickness="5,5,5,5" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="100,100,0,0">
<StackPanel>
<StackPanel.Background>
<ImageBrush ImageSource="/Pictures/circuit-1242111.jpg"/>
</StackPanel.Background>
<TextBox Margin="20,10,20,0" BorderBrush="{x:Null}" Background="{x:Null}" Text="103/250" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontSize="16"></TextBox>
<ProgressBar Margin="0,10,0,0" Height="10"></ProgressBar>
<Grid>
<Grid.Background>
<SolidColorBrush Color="#FF8A8A8A" Opacity="0.85"/>
</Grid.Background>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20" Text="Circuits"></TextBlock>
</Grid>
</StackPanel>
</Border>
<Border BorderBrush="#FF3CFF00" BorderThickness="5,5,5,5" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="100,100,0,0">
<StackPanel>
<StackPanel.Background>
<ImageBrush ImageSource="/Pictures/circuit-1242111.jpg"/>
</StackPanel.Background>
<TextBox Margin="20,10,20,0" BorderBrush="{x:Null}" Background="{x:Null}" Text="12/300" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontSize="16"></TextBox>
<ProgressBar Margin="0,10,0,0" Height="10"></ProgressBar>
<Grid>
<Grid.Background>
<SolidColorBrush Color="#FF8A8A8A" Opacity="0.85"/>
</Grid.Background>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20" Text="Switches"></TextBlock>
</Grid>
</StackPanel>
</Border>
<Border BorderBrush="#FF3CFF00" BorderThickness="5,5,5,5" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="100,100,0,0">
<StackPanel>
<StackPanel.Background>
<ImageBrush ImageSource="/Pictures/circuit-1242111.jpg"/>
</StackPanel.Background>
<TextBox Margin="20,10,20,0" BorderBrush="{x:Null}" Background="{x:Null}" Text="347/500" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontSize="16"></TextBox>
<ProgressBar Margin="0,10,0,0" Height="10"></ProgressBar>
<Grid>
<Grid.Background>
<SolidColorBrush Color="#FF8A8A8A" Opacity="0.85"/>
</Grid.Background>
<TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="20" Text="Resistors"></TextBlock>
</Grid>
</StackPanel>
</Border>
</StackPanel>
Here is the background picture I used:

WPF c# a button with a textbox

I need to create a button similar like below, it is a textbox and a button combined, you can click "Get Code" button and you will receive a SMS text with the code, then you can input the code in the textbox.
textbox and a button
anyone can help?
Hi, my current code is quite simple.
<Button
Grid.Row="2"
Grid.Column="0"
HorizontalOptions="Center"
Text="Get Code"
FontFamily="{ StaticResource IconsFontFamily }"
Style="{ StaticResource CircleActionButtonFlatStyle }"
micro:Message.Attach="[Event Clicked] = [Action SendVerifyCodeAsync()]"/>
<Entry
Grid.Column="1"
Grid.Row="2"
Grid.ColumnSpan="2"
VerticalOptions="Center"
HorizontalOptions="FillAndExpand"
Text="{Binding Code}"
Placeholder="Send Code" />
Has people said in comments, you need to wrap it in borders.
With this example, you just need to change the color for the ones you want, and then you can set Click events in the respective borders to make them act like buttons and do the respective actions. You can add the code micro:Message.Attach="[Event Clicked] = [Action SendVerifyCodeAsync()]" to the respective border.
Code:
<Border BorderBrush="Black" BorderThickness="1" Margin="10" CornerRadius="15">
<Grid Margin="-1">
<TextBox TextWrapping="Wrap" Text="Input code" Background="{x:Null}" BorderBrush="{x:Null}" HorizontalAlignment="Left" Margin="5,0,0,0" VerticalAlignment="Center"/>
<Border BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Right" CornerRadius="15" Background="#FF39D3D3">
<TextBlock TextWrapping="Wrap" Text="Get Code" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,0"/>
</Border>
</Grid>
</Border>
Example:

Horizontal alignment in a button

Here's my code for the button:
<Button //namespaces
x:Class="myProject.MyButton"
FocusVisualStyle="{x:Null}"
Foreground="White"
Height="60"
Width="200"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
Padding="0 0 1 1"
>
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" CornerRadius="8">
<Border.Background>
#006BB7
</Border.Background>
<DockPanel Margin="3">
<Image Source="{Binding Path=Tag, RelativeSource={RelativeSource TemplatedParent}}" Height="30" Stretch="Fill" Margin="0 0 0 0" HorizontalAlignment="Left"/>
<TextBlock Text="{Binding Path=Content,RelativeSource={RelativeSource TemplatedParent}}" FontSize="20" VerticalAlignment="Center" Padding="10,0,10,0" HorizontalAlignment="Left"/>
<Image Source="{DynamicResource ResourceKey=Bmp}" Height="30" Width="30" HorizontalAlignment="Right" Margin="0 0 10 0"></Image>
</DockPanel>
</Border>
</ControlTemplate>
</Button.Template>
</Button>
When I call it like this:
<ui:MyButton x:Name="btnGo" Tag="/Resources/Go.png" Content="Go" Command="{Binding Path=GoCommand}" Margin="20 0 0 0"/>
and everything looks like I want it to look: the first image isn't shown, and the third part of the button (the second image) is aligned right.
However, here:
<ui:MyButton Margin="10" Content="{Binding MyName}" Tag="{Binding Path}" HorizontalAlignment="Center" VerticalAlignment="Top">
<Button.Resources>
<BitmapImage x:Key="Bmp" UriSource="/Images/arrowRight.png"></BitmapImage>
</Button.Resources>
</ui:MyButton>
Whn I call the button like this, the text and the second image aren't shown. All that is shown is the first image and it is stretched via the whole button's width. Why is this happening? I want to make this to behave as the other one.
Try this
In Change content of Image and of TextBlock when the Image and the TextBlock are in a button we are using resource for button and here we are using for <ui:MyButton>
<ui:MyButton Margin="10" Content="{Binding MyName}" Tag="{Binding Path}" HorizontalAlignment="Center" VerticalAlignment="Top">
<ui:MyButton.Resources>
<BitmapImage x:Key="Bmp" UriSource="/Images/arrowRight.png"></BitmapImage>
</ui:MyButton.Resources>
</ui:MyButton>

How can I set opacity of a textbox to 1 when it is within a border with opacity 0.5?

I have the following xaml:
<Border x:Name="baseBorder" Grid.Row="0" Grid.RowSpan="200" Canvas.ZIndex="1" Opacity="0.5" Background="Gray">
<Border x:Name="interiorBorder" Background="White" Height="200" Width="450" Opacity="1">
<TextBlock x:Name="txtMessage" HorizontalAlignment="Center" Width="400" TextAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Center" FontSize="20" FontWeight="Bold" Foreground="Black" >
</Border>
</Border>
I want the interiorBorder to have opacity 1 while maintaining the base border's opacity 0.5.
The above xaml is not working, I'm getting 0.5 opacity in both objects.
Try making interiorBorder a sibling of baseBorder instead of a child, perhaps something like this.
<Border x:Name="baseBorder" Grid.Row="0" Grid.RowSpan="200" Canvas.ZIndex="1" Opacity="0.5" />
<Border x:Name="interiorBorder" Grid.Row="0" Grid.RowSpan="200" Canvas.ZIndex="1" Opacity="1">
<TextBlock x:Name="txtMessage" />
</Border>
I imagine you'll also want to set Margin on interiorBorder so you'll still be able to see baseBorder.

WrappingText in stackpanel

I have a stackpanel with 2 textblocks. The problem is that my textblocks doesn't wrap a text.
What is weird, even if I have only one textblock wrapping also doesn't work.
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" Grid.Row="1" Grid.ColumnSpan="2" Margin="0">
<!--<ContentControl Width="800">-->
<TextBlock x:Name="textBlock" TextWrapping="Wrap" Text="{Binding Path=StaticTextLibrary.ApplicationSubtitle, Source={StaticResource ResourcesManager}}"
FontSize="14" HorizontalAlignment="Left" Foreground="{StaticResource text}" Opacity="0" RenderTransformOrigin="0.5,0.5"
VerticalAlignment="Bottom" Padding="5" Height="30">
<TextBlock.RenderTransform>
<CompositeTransform TranslateX="30"/>
</TextBlock.RenderTransform>
</TextBlock>
<!--</ContentControl>-->
<TextBlock x:Name="subTitlePostfix" TextWrapping="Wrap" Text="by Credit SuisseĀ©" Margin="6,0,0,0" Opacity="0" RenderTransformOrigin="0.5,0.5" VerticalAlignment="Top" HorizontalAlignment="Left" Foreground="{StaticResource text}" Padding="0,4,0,0" >
<TextBlock.RenderTransform>
<CompositeTransform TranslateX="30"/>
</TextBlock.RenderTransform>
</TextBlock>
</StackPanel>
Where have I done a mistake?
Why don't you use a WrapPanel from the Silverlight ToolKit?
I copied to code and punched it until it would wrap.
Here are a few notes and a solution:
Your first TextBlock specifies a Height, but 30 is not enough for a second line ==> can't wrap because can't create a second line. Remove Height="30".
Instead of StackPanel, don't you mean WrapPanel? unless you have something special in mind.
I used TranslateTransform x="30" instead of CompositeTransform TranslateX="30", it shouldn't be an issue though.
HTH,
bab.

Resources