how to insert image dynamically in textbox wpf - wpf

we often see any ajax based website where textbox is placed for searching data. when we write something in the textbox and when data is fetching then a spinner gif images is shown in the textbox with right align. i know this done via css. so how can i achieve the same effect in wpf where textbox will be there and user will write something in the textbox and when data will be fetch then a spinning gif image will show in the textbox with right align and when data fetch will be completed then image will be invisible. please guide me with sample code if possible. thanks

To get you started, look at Data Validation in WPF, specifically, look for the section on Providing Visual Feedback with the AdornedElementPlaceHolder class in WPF.
<AdornedElementPlaceholder/>
It is my belief that this mechanism could be relatively easily adapted to solving your particular problem.
Edit: Additional info
In your case, you'll create a control template for a TextBox and assign that control template to your textbox element. This example varies from the example in the link by changing the TextBox to an Image element. The TextBox in the example in the link is the red character that represents some sort of validation error.
<ControlTemplate x:Key="validationTemplate">
<DockPanel>
<Image Source="<location of your animated gif>">
<AdornedElementPlaceholder/>
</DockPanel>
</ControlTemplate>

Related

What do you call a UI element which expands an area of fields in a window?

I am trying to make UI element which expands/collapses an area of fields in a window. I need it specifically for a WPF project.
As an example take the element in the link below.
https://i.stack.imgur.com/MDgNl.png
Pushing the down button expands the area to include more fields as in the example below
https://i.stack.imgur.com/jN6bO.png
Does this kind of UI element have a name? I don't really know what to google to see whether it has been done before.
I think you are looking for the Expander control:
<Expander>
<Grid>
<TextBlock>put your controls to be expanded/collapsed here...</TextBlock>
</Grid>
</Expander>

WPF TextBox border not changing properly with validation [duplicate]

After going through several articles and examples showing usage of
AdornedElementPlaceholder
i am still confused that what is the exact functionality it incorporates to xaml validation?
If you use Validations you need to show the user where (and what) failed to validated and that’s where AdornedElementPlaceholder comes into play. It is a Placeholder that has exactly the same size of the UIElement you’re validating.
Let’s say you’re validating user input on a TextBox and want to show a red box around the TextBox when the validation fails. Define a ValidationRule and a validation template for the TextBox. If the ValidationRule fails, then the Validation.ErrorTemplate is shown on your TextBox. Inside the template the AdornedElementPlaceholder tells the Framework where to place your template on the UI. In our case the template might look like this:
<ControlTemplate>
<Border BorderBrush="Red" BorderThickness="1">
<AdornedElementPlaceholder />
</Border>
</ControlTemplate>
You should read this article.
I believe the AdornedElementPlaceholder is used to show where the "validation indicator element" is positioned in relation to the control being validated.
I.e. if you have a text box and when validation fails a red x appears on the right of the box, I believe (although not 100%) that the AdornedElementPlaceholder is responsible for that positioning.
Represents the element used in a ControlTemplate to specify where a
decorated control is placed relative to other elements in the
ControlTemplate.
Source:
http://msdn.microsoft.com/en-us/library/system.windows.controls.adornedelementplaceholder(v=vs.110).aspx

WPF Markup Control

I have a string of text that I need to display. I currently display it in a textbox.
My requirements have changed and now I need to display parts of this string in Red.
I have looked and all I can see for displaying text in several colors is a WebBrowser or RichTextbox. Both of these are more complex then I was hoping to use.
I can format the text string in any way (using any kind of markup).
Is there some kind of simple markup control out there for wpf? (Note: This will be going on a datagrid that can have many hundreds of rows, so it cannot be a memory/processing intensive control.)
You can use a TextBlock if it doesn't need to be edited.
From above link:
TextBlock is designed to be lightweight, and is geared specifically at integrating small portions of flow content into a user interface (UI). TextBlock is optimized for single-line display, and provides good performance for displaying up to a few lines of content.
TextBlock is not optimized for scenarios that need to display more than a few lines of content; for such scenarios, a FlowDocument coupled with an appropriate viewing control is a better choice than TextBlock, in terms of performance. After TextBlock, FlowDocumentScrollViewer is the next lightest-weight control for displaying flow content, and simply provides a scrolling content area with minimal UI. FlowDocumentPageViewer is optimized around "page-at-a-time" viewing mode for flow content. Finally, FlowDocumentReader supports the richest set functionality for viewing flow content, but is correspondingly heavier-weight.
Modified example from link showing Red Text:
<Grid>
<TextBlock TextWrapping="Wrap" >
<Bold>TextBlock</Bold> is designed to be <Italic>lightweight</Italic>
and is geared <Run Foreground="Red">specifically</Run> at integrating
<Italic>small</Italic> portions of flow content into a UI.
</TextBlock>
</Grid>
AvalonEdit should do what you want. There is a Nuget package for it. See here also:
http://www.codeproject.com/Articles/42490/Using-AvalonEdit-WPF-Text-Editor
Also, if the text is not being edited, I think you should probably use something like a ListBox with custom item templates -- especially if the entire line is to be highlighted.

Lightbox-style popup in WPF -- how?

(I am trying to learn WPF using tutorials and documentation, and trying to develop a user interface for my backend-complete application while I do say. I've heard people say that the learning curve is quite steep. But sometimes I wonder whether what I'm trying to do is actually something that's hard to do in WPF, or if it's simple but I'm thinking in wrong terms, or if it's neither, it's quite simple but I just happen not to know how.)
Here's my current question. I wanted clicking that clicking some part of my UI will bring up a 'popup' where the user can enter more information. I would like a 'lightbox-style' popup, i.e. the popup is modal to the page, it darkens the rest of the page to become the center of attention, etc. These are seen commonly on Web sites.
A bit of searching led me to the WPF Popup control. I added it, put my content in, set the IsOpen property to True, and -- presto! A popup. Then I added an invisible Rectangle that covers my whole window, and set it to Visible as well when I want my popup to open. Great!
So now I wanted to do this dynamically, because sometimes I will be loading a record which will sometimes have a need to open another control (a UserControl) in a popup to edit its information. So I made myself a method called OpenPopup. But I can't seem to find a way to write this method using WPF. In Windows Forms I'd have written: (I use VB.NET)
Sub ShowPopup (form as Form, ctrl as Control)
'Create 'rect' as new dark rectangle control
'Z-order it to the top
'form.Controls.Add 'rect'
'form.Controls.Add ctrl
'Z-order 'ctrl' to the top
'Center 'ctrl'
'Set focus to it
End Sub
But with WPF I run into problems:
1) I can't add it to the WPF window, because it already has a child.
2) If that child is a Canvas, that's not too bad. I can detect that, and add it to the Canvas. I have to find some way to set its Left, Top etc. properties and Width and Height, since those do not seem to be properties of the Rectangle control but rather extended by the Canvas object -- in XAML they're called Cavnas.Top etc. but Intellisense is not showing them when I try to use it in code.
3) But if it's a StackPanel? Then my rectangle will just be stacked below the other controls! And not covering them! Is there a way around this?
4) And if the window contains only one control and no container control at all?
5) I think there were more problems I ran into. But let's start with these.
Thanks in advance for your help.
1) I can't add it to the WPF window, because it already has a child.
Ah, the evils of codebehind. The solution is not to add it to the visual tree, it is to place it in the visual tree, ready and waiting to pounce, but hide it from the user's view.
Here's a sample you can drop in Kaxaml that demonstrates the point. Set the Lightbox Grid's Visibility to Hidden to access the hidden content.
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Viewbox>
<TextBox Text="SIMULATING CONTENT" />
</Viewbox>
<Grid x:Name="Lightbox" Visibility="Visible">
<Rectangle Fill="Black" Opacity=".5"/>
<Border
Margin="100"
Background="white"
BorderBrush="CornflowerBlue"
BorderThickness="4"
CornerRadius="20">
<Viewbox Margin="25">
<TextBox Text="SIMULATING LIGHTBOX"/>
</Viewbox>
</Border>
</Grid>
</Grid>
</Page>
2) (snip) Intellisense is not showing them when I try to use it in code.
Canvas.Top etal are Attached Properties. Attached Properties are extremely convenient and easy to use in XAML, but they are very confusing and hard to use from code. Another reason why codebehind is evil.
3) But if it's a StackPanel? Then my rectangle will just be stacked below the other controls! And not covering them! Is there a way around this?
I redirect you back to 1. There are also many other container controls in WPF. You should investigate them and observe how they control layout. For instance, my use of the Grid was not to make use of its ability to block off sections of UI for controls, but for its ability to layer controls ontop of each other and to stretch them out to their maximum available size for the available space (the viewboxes are just there to zoom the controls instead of stretch them).
4) And if the window contains only one control and no container control at all?
The root of a window would almost always be a container control. But you control that, so if you needed to add controls to the visual tree at runtime you could easily ensure the child of the window is a container control you could deal with.
5) I think there were more problems I ran into. But let's start with these.
No kidding. My number one suggestion for people in your situation is to drop what you're doing and learn about MVVM. The Model-View-ViewModel is a very simple way to code WPF applications that takes advantage of many of the features of WPF--databinding, templating, commands, etc. It allows you to code your logic not in codebehind (RETCH) but in easy to create and test classes.

How to control placement ordering when using arrangeoverride to extend WPF controls

I am extending the WPF textbox in order to place a custom image on it. I want to capture mouse clicks on this image. So far i have been able to use arrangeOverride to place the image in where i need it but since it happens to be placed "inside" the text box, whenever i try clicking it the text box captures the click and the event that is attached to the image does not fire. Is it possible to indicate that the image should be placed on top of the text box when using arrange override? I know i can get around this issue if i extended a Control and placed a text box inside it, but for my application i need to actually extend a text box in order to be able to use it in another more complex control that is currently using a text box.
Thanks!
ArrangeOverride may not be the ideal solution for this. A better approach would probably be to use control templating: create a ControlTemplate containing a Grid with a single cell, and place the Image and the text box content host in that single cell. You can place the Image on top implicitly by the order you place the controls, or explicitly by setting the Panel.ZIndex attached property.
The one trick here is knowing how to represent the text box content host. This has to be a ScrollViewer with the name PART_ContentHost. So your template will look something like:
<ControlTemplate TargetType="{x:Type Ambog36sTextBox}">
<Grid>
<ScrollViewer x:Name="PART_ContentHost" /> <!-- WPF will fill in a text edit area here -->
<Image Panel.ZIndex="1" Source="..." />
</Grid>
</ControlTemplate>
Then assign this template to your custom text box class via the default style in the usual way.
As you are not using the standard composition model it means you need to override the mouse handling before it reaches the text box code and add your own custom logic to decide on the events to generate.

Resources