Is there a way to disable dropping items like notepad, word etc on to a System.Windows.Controls.WebBrowser control in WPF.
I have tried various options like
AllowDrop="False" - Does not work.
Tried to catch events like Drop, PreviewDrop, DragLeave, PreviewDragLeave, PreviewDragOver, PreviewDragEnter - None of these events fire when I drop items on the WebBrowser control.
Using the constructor or Xaml to create the following method will stop the drag from changing the webbrowsers current state:
private void webBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
{
// The WebBrowser control is checking the Uri
if (e.Uri.ToString() != "Place your url string here") //ex: "http://stackoverflow.com"
{
// Uri is not the same so it cancels the process
e.Cancel = true;
}
}
As you mentioned, none of the Drop event are fired, and it seems like the native browser control is very limited which leaves you with two possibilities:
Either user another browser control like wpfchromium,
Or define a transparent popup in top of the browser and handle
the drop even within it
:
<WebBrowser Height="300" Width="300" x:Name="wbBrowser"/>
<Popup Opacity="1" AllowsTransparency="True" IsOpen="True" Placement="Center"
PlacementTarget="{Binding ElementName=wbBrowser}" >
<Border Background="Black" Opacity="0.01"
Width="300" Height="300">
</Border>
</Popup>
This solution ain't pretty at all and needs also more logic to handle when the popup's parent control moves..
...you may also find this solution helpful, if your main content is html:
How to disable drop on wpf webbrowser control
Related
Hey I use the RDPCOMAPILib to shadow screens between two computers.
I added a windows forms user control to my project which include the axRDPViewer.
This Control is added to my WPF View in a windowsFormsHost control and works fine, see code:
<Grid Name="RDP">
<Border BorderBrush="Black" BorderThickness="2">
<WindowsFormsHost MouseDown="host_MouseDown" PreviewMouseDown="host_PreviewMouseDown" x:Name="host">
<controls:RDPViewer Click="viewer_Click" MouseClick="viewer_MouseClick" x:Name="viewer"/>
</WindowsFormsHost>
</Border>
</Grid>
But none of the click events is raised when I click on the control, but I don't know why?
I want to do something when I doubleclick on the control.
Hope somebody knows a solution to my problem.
I don't know anything about the library or control you're using, but you seem to be missing a " in the code you posted. You have
MouseClick="viewer_MouseClick x:Name="viewer"
but it should be
MouseClick="viewer_MouseClick" x:Name="viewer"
Why WebBrowser component does not allow other components (button, textbox, etc) is created over it? I was using Popup to contain the components that need to use, but to move the program screen popups do not follow the screen remain static in the place they were created.
<Grid>
<WebBrowser x:Name="wbBrowser" />
<Canvas ClipToBounds="False">
<Popup PlacementTarget="{Binding ElementName=wbBrowser}" IsOpen="true" AllowsTransparency="True" >
<Button>asdasdasdsadasd</Button>
</Popup>
</Canvas>
</Grid>
The WebBrowser control in WPF is actually a wrapped version of the WebBrowser control from WinForms. As such it is always rendered above other WPF controls. The only things that you can show above would be another window.
The WebBrowser control is pretty limited in WPF, I believe it uses IE8, so there is no HTML5 support. There are some open source browser controls for WPF that are newer that likely don't suffer from the same limitations of the built in WPF control. Take a look at this project: https://wpfchromium4.codeplex.com/
I have a textbox with a button inside (Telerik's RadTextBox with an Action configured).
When the user presses the Action, a progress bas is displayed, the screen goes dark, and some magic happens.
My problem is that since the action doesn't result in the textbox losing focus, the on-screen keyboard is not hidden, and keeps covering half the screen.
I would like to programmatically hide the on-screen keyboard, but don't know how.
Just set focus to the main page:
this.Focus();
this will focus a control that doesn't use the keyboard and thus hide the keyboard. Unfortunately there is no API to the keyboard to hide it.
Instead try disabling and then enabling the textbox in question in an appropriate place (like once a query has been submitted or an action triggered):
TextBox.IsEnabled = false;
TextBox.IsEnabled = true;
(Via https://stackoverflow.com/a/23905874/1963978)
Not clean, but it does the job (in Windows 10 mobile).
here lot solution is available for a Textblock only but in my case AutoCompleteBox
<toolkit:AutoCompleteBox Name="autoComplateTxt"
Grid.Row="4"
Margin="15,5,2,10"
Padding="0"
Height="65"
Text=""
BorderThickness="1"
BorderBrush="Black"
VerticalAlignment="Center"
DropDownClosed="autoComplateTxt_DropDownClosed"
/>
private void autoComplateTxt_DropDownClosed(object sender, RoutedPropertyChangedEventArgs<bool> e)
{
this.Focus();
}
i'm stuck with airspace problem. My WPF app hosts a winform component. I want to display a popup with some "waiting-please" text during component loading and long activities. Here I get my problem: popup is correctly display but when I handle component's busy event I cannot update popup content. Here some code XAML:
<WindowsFormsHost Name="wfh" Grid.Row="1" Grid.Column="0" ></WindowsFormsHost>
<Popup x:Name="Overlay" AllowsTransparency="True" Placement="Center"
StaysOpen="True"
IsOpen="True"
PlacementTarget="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=Grid, AncestorLevel=1}}">
<TextBlock Name="tbWait" Foreground="Red" />
</Popup>
and c#:
myWinformComponent.Event => (s, e) =>
{
tbWait.Text = e.IsBusy ? "Loading..." : string.Empty;
}
I know what is the Airspace problem with WinForm and WPF but I was supposed that keep the popup always open let me display any content ove the windowformshost.
EDIT: I'm placing some breakpoints into the code behind and I see the Text property change correctly. This changes are not display into the UI.
Have you any workaround or solution?
Thank you guys!
According with #HansPassant comment, I can force redraw with these lines:
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background, new DispatcherOperationCallback(delegate(object parameter) {
frame.Continue = false;
return null;
}), null);
Dispatcher.PushFrame(frame);
Maybe it's not the most elegant solution, but it's a working patch to my issue.
Unfortunately, you can't display any WPF content over a WindowsFormsHost. I spent a long time fighting this quirk myself, until I finally gave up and rearraged the UI to make the overlay not have to be over the WinForms component.
Why doesn't keyboard input work for a ScrollViewer when the child control has input focus?
This is the scenario. A WPF window opens. It sets the focus to a control that is embedded in a ScrollViewer.
I hit the up and down and left and right keys. The ScrollViewer doesn't seem to handle the key events, anyone know why?
This is the simplest possible example:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
FocusManager.FocusedElement="{Binding ElementName=control}"
>
<Grid>
<ScrollViewer
HorizontalScrollBarVisibility="Auto"
>
<ItemsControl
x:Name="control"
Width="1000"
Height="1000"
/>
</ScrollViewer>
</Grid>
</Window>
When you start the app that contains this window, "control" appears to have the focus as I intended. Pressing the key seems to result in bubbling key events reaching the ScrollViewer (I checked for this using WPF Snoop). I can't work out why it doesn't respond to the input.
The problem
A ScrollViewer ignores all KeyDown events whose OriginalSource is not the ScrollViewer. The OriginalSource on a KeyDown is set to the focused control, therefore the ScrollViewer ignores it when a child has the focus.
The solution
Catch the KeyDown event and raise a copy of it directly on the ScrollViewer so it will have the correct OriginalSource, like this:
void ScrollViewer_KeyDown(object sender, KeyEventArgs e)
{
if(e.Handled) return;
var temporaryEventArgs =
new KeyEventArgs(e.KeyboardDevice, e.InputSource, e.Timestamp, e.Key)
{
RoutedEvent = e.RoutedEvent
};
// This line avoids it from resulting in a stackoverflowexception
if (sender is ScrollViewer) return;
((ScrollViewer)sender).RaiseEvent(temporaryEventArgs);
e.Handled = temporaryEventArgs.Handled;
}
the event handler can be added in XAML:
<ScrollViewer KeyDown="ScrollViewer_KeyDown" />
or in code:
scrollViewer.AddHandler(Keyboard.KeyDownEvent, ScrollViewer_KeyDown);
The latter is more applicable if the ScrollViewer is inside a template somewhere and you have code to find it.
In order for the ScrollViewer to react to your keyboard keys - it needs to have IsFocused="True" - right now it's child has the focus.
To prove it - try in your Loaded event to manually set focus to the ScrollViewer (you might have to set IsFocusable="True" for it to work on the ScrollViewer) - now the keys should work. If you want it to work otherwise, you need to set the appropriate EventHandlers on the ScrollViewer (KeyDown/KeyPress etc.)
Similar issue with no keyboard navigation when using ScrollViewer inside an ItemsControl.Template. Adding Focusable and IsTabStop resolved the issue in my case.
<ScrollViewer
Focusable="True"
IsTabStop="True">
<ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" />
</ScrollViewer>