I want to put a small icon (a png file) in to the corner of a textbox. A sketch of what I mean is at http://dosketch.com/view.php?k=mIPeHLSBLx0e2YpoVpYO
So far I've got (in my main xaml file)
<TextBox Style="{StaticResource IconTextBox"} ...
and in my resources area:
<Style x:Key="IconTextBox" TargetType="TextBox">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Image Source="icon.png" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Obviously this isn't working (I'm losing the textbox for a start!). Ideally when the user is typing into the textbox, the text doesn't overlap the image - but for the time being I'll just be happy getting the image in the textbox.
Thanks
Instead of overwriting the template, I would create a new simple UserControl:
//pseudocode, does not compile!
<UserControl x:Class="MyUserControl" x:Name="root">
<Grid>
<!-- put some effort in aligning the items so it looks nice -->
<TextBox Text="{Binding ElementName=root, Path=Text}" />
<Image Source="{Binding ElementName=root, Path=IconSource}" />
</Grid>
</UserControl>
public class MyUserControl : UserControl{
dependencyproperty string Text;
dependencyproperty string IconSource;
}
so you can use it as follows:
<my:MyUserControl Text="{Binding MyText}" IconSource="{Binding MyIcon}"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With