Can I place controls inside Textbox in WPF? - wpf

For example Button, Link, Image ,...
This code does not work:
<Grid>
<TextBox
HorizontalAlignment="Left"
Height="180"
Margin="38,35,0,0"
TextWrapping="Wrap"
Text="TextBox"
VerticalAlignment="Top"
Width="442"
>
<Button
Content="Button"
HorizontalAlignment="Left"
Margin="149,105,0,0"
VerticalAlignment="Top"
Width="75"
RenderTransformOrigin="-2.78,1.376"
/>
</TextBox>
</Grid>

Not without writing your own custom control. That is the proper solution.
Here is a good example of a custom control for this: https://stackoverflow.com/a/6419556/1624581
Alternatively, as a hacky and quick solution you could put the button and the textbox in a container (e.g. stackpanel or grid) and then use a negative margin on the button so that it appears within the textbox. You can then apply a padding on the right of the textbox so that the input text doesn't overlap with the button. With that said, I'd strongly recommend writing a custom control as negative margins can cause maintenance headaches in the long run.

Related

WPF Button Content VerticalAlignment on default button

I'm not able to center text vertically on a default button (no styles or templates used)
I saw threads like
Text content in a WPF button not being centred vertically
^^ I'm not setting any text height atleast in button 1.
Not sure what causes this behavior and how to get rid of it efficiently when localisation comes in.
I do get I can set a -ve margin to align text exactly, however that might screw things up for another language. Is this some limitation based on the Default Font Family Expression blend seems to impose?
Any help with this would be great. Seems trivial but cant seem to find a decent explanation for what's causing this behavior.
Xaml code is as follows:
<Window x:Class="TestButton.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="ButtonFFAH" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" VerticalContentAlignment="Center"/>
<Button Content="ButtonAH" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"
Margin="80,0,0,0" Height="15.627" VerticalContentAlignment="Center"/>
</Grid>
It`s how it works internally. If you want some more control, just fill button content with more customizable things that just a string. Like this:
<Button HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"
Margin="80,0,0,0" Height="15.627" VerticalContentAlignment="Center">
<TextBlock VerticalAlignment="Center">ButtonAH</TextBlock>
</Button>
The apparent vertical misalignment is due to the fact that all kinds of accents or other diacritical marks add to the total font height. Although the actual Button content may not contain such characters, the vertical alignment has to take this into account to ensure a common baseline alignment with other Buttons with the same "outer" alignment, for example in the same Grid row.

How to create tiger textbox in silverlight

I want to add the tiger textbox for message conversation in my project, I dont know how to apply styles or controls to get the tiger textbox in silverlight.
Is any way to get the above image in silverlight code / styles
Short of creating your own custom control in silverlight, there is no way you can have a textbox shaped like that.
However, there are 2 ways you can get what you want.
1) Either add a simple Border to your textbox. Change the opacity setting according to your needs.
For example:
`<Border BorderBrush="Black" BorderThickness="3" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="49,33,0,0">
<TextBox Height="72" TextAlignment="Center" Name="textBox1" Text="TextBox" Width="270" Background="White" />
</Border>`
2) Place the Image of the bubble, and place a textbox inside it. You would have to change a few opacity settings, but here is an example:
<Grid HorizontalAlignment="Left" Height="96" Width="316" Margin="99,265,0,0" VerticalAlignment="Top">
<Image Name="image1" Stretch="Fill" Source="bubble.png" />
<TextBox Name="textBox2" Text="TextBox" SelectionBackground="#001BA1E2" SelectionForeground="Black" BorderBrush="Transparent" Background="Transparent" Margin="0,0,0,30" />
</Grid>
You would have to made a few adjustments with the margins based on the image you use. But basically, this is how the above 2 textboxes would look:
If You wish to create your own Control for the Tiger Textbox, you could follow these steps:
1) Open the Project in Expression Blend
2) In the position where you wish to add the textbox, Draw an image which is in the Tiger Textbox format.
3) Right Click on this Image and Select "Make into Control..."
4) Choose the TextBox.

Why does WP7 ListPicker have different margins and height to TextBox

I have a page in my WP7 app consisting of a TextBox beside a ListPicker. In their default modes, they don't line up properly; the ListPicker has a different padding to the TextBox, and its height is also different.
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<StackPanel Orientation="Horizontal">
<TextBox HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top"/>
<toolkit:ListPicker Width="170" ItemsSource="{Binding l}" Style="{StaticResource ListPickerStyle1}" VerticalAlignment="Top"/>
</StackPanel>
</Grid>
Manually tweaking the ListPicker's template to fit in correctly here is tricky and error prone. For example, when its height is adjusted, the caption (i.e. the text of the selected item) is no longer in the centre of the component.
My app is currently failing MS app review because the components are not all the same height.
Is there an easy way for me to set the toolkit:ListPicker to have the same appearance as a TextBox?
The simplest solution will be to take a copy of the the default style and tweak that using Blend to be exactly how you want it to look. This will take a little trial and error to sort out.
You can then use the implicit styling rules to apply it to all ListPickers without having to explicitly set the style on each instance:
<Style x:Key="MyListPickerStyle
TargetType="toolkit:ListPicker>
.... your tweaks here
</Style>
<Style TargetType="toolkit:ListPicker"
BasedOn="{StaticResource MyListPickerStyle}" />
It may be easier to tweak the TextBox Style of course :)

WPF: How to make an overlay control with transparent canvas and clickable child

I want to make a custom control which will be used as an overlay. The control should contain a couple of child controls which should be drawn and should be clickable as usual. But everything else in the control should be transparent and "clickable-through".
Here is how I try to achieve this... First, I'm using PreviewMouseDown\Up\Move events in the window where the overlay is going to be placed. I want these events to "go through" transparent part of my custom control, but stop at not-transparent (for example at my button). Second, here is the xaml for my control (root UserControl node was left untouched):
<Canvas Background="transparent" IsHitTestVisible="true">
<Button Canvas.Left="384" Canvas.Top="34" Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" IsHitTestVisible="True" />
<TextBlock Canvas.Left="27" Canvas.Top="105" Height="36" Name="textBlock1" Text="TextBlock" Width="432" FontSize="24" IsHitTestVisible="False" Foreground="Red" FontWeight="Bold" />
</Canvas>
However if I set Canvas' IsHitTestVisible to false, the whole control including button becomes "unhittable". If set the it to true my all the tunneling events stop at custom control and button becomes unclickable.
What is the right way to achieve this kind of behavior?
Is it possible to do so without subclassing canvas (or any other panel)?
You should set the background of the Canvas to null (or just no background, null is default). Transparent is "visible" to the mouse clicks.

Visual Studio WPF designer wont line up

Ive done searching and i can't find anything on why my designer window does this.
http://i.imgur.com/bH4lU.png
and when i run it looks like this.
http://i.imgur.com/oOhYt.png
See how it is farther away on the right?
This makes building a window very annoying. How can i stop this?
<Window x:Class="WPF1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Main Window" Height="360" Width="547" ResizeMode="NoResize">
<Grid Background="#FF464646" Height="329">
<ListBox Height="321" HorizontalAlignment="Left" Name="listBox1" VerticalAlignment="Top" Width="120" Background="#FF252525" />
<Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="126,298,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
<ListBox Height="322" HorizontalAlignment="Left" Margin="393,0,0,0" Name="listBox2" VerticalAlignment="Top" Width="132" BorderBrush="White" Background="#FF252525" />
</Grid>
You have the 2nd ListBox left aligned with a 393 margin. Set the HorizontalAlignment to Right instead.
Judging by your xaml, it looks like you are using the Visual Studio designer to create this code. You should study up on WPF's layout system some more and this will all make sense. Currently your view is static, which means it won't resize with the parent element (i.e. when the Window resizes, all of the stuff stays in the same place). This is why you are seeing borders. You'll want to remove the Height, Width, and Margins on all of your elements, and set the HorizontalAlignment and VerticalAlignments accordingly to make your Window look right when it is resized.

Resources