I have an WPF User control which is is hosted in an Elementhost. I use elementhost to include an WPF user control in my classical Windows forms app.
Now, from Windows forms side I am trying to capture the mouseDown event that is produced in an WPF label but I don't know how to do it.
Any ideas?
A case might be able to help you. The winform form calls the wpf control.
Create a WPF custom control. The xaml code of the control is as follows.
<Grid>
<Image Margin="10,10,10,90" x:Name="img" Stretch="Uniform" Opacity="1">
<Image.BitmapEffect>
<DropShadowBitmapEffect Opacity="1" />
</Image.BitmapEffect>
</Image>
<TextBox Background="Transparent" Foreground="White" Height="40" FontSize="32" Margin="44,0,56,36" x:Name="txtBox1" Opacity="0.5" Text="" VerticalAlignment="Bottom" /> </Grid>
You need to add the corresponding function to set the effect. The code is as follows.
public void SetSource(string fileName)
{
img.Source = new BitmapImage(new Uri(fileName) );
}
public void SetOpacity(double opacity)
{
img.Opacity = opacity;
}
//
public string GetText()
{
return txtBox1.Text;
}
Create a Winform application and add a reference, otherwise the control will not work properly. The list of references is pictured below.
Regenerate the solution. On the left toolbar, a WPF control appears and drag it to the form.
Use the button control in the winform project to call the corresponding function.
private void button1_Click(object sender, EventArgs e)
{
((UserControl1)elementHost1.Child).SetSource(#"C:\Users\Admin\Pictures\Saved Pictures\9837f99502eba3d01d4fb671cab20c15.jpg");
}
private void button2_Click(object sender, EventArgs e)
{
((UserControl1)elementHost1.Child).SetOpacity(0.5);
}
private void button3_Click(object sender, EventArgs e)
{
string text = ((UserControl1)elementHost1.Child).GetText();
label1.Text = text;
}
Test items: The left side is the traditional Winform control. The right side is the imported WPF control. You can clearly see the "translucent" effect of the picture.
Not sure what exactly you're trying to achieve. Below is a simple example.You can edit the MouseDown event of the UserControl as needed.
If there is a problem, please make your problem clearer and show me the complete code sample that can reproduce your problem for analysis.
UserControl:
<Grid>
<Label x:Name="label" Content="Label" MouseDown="label_MouseDown" Background="AliceBlue" Width="300" Height="200" />
</Grid>
private void label_MouseDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("hello");
}
Add the UserControl reference in the WinForms project, drag and drop the UserControl on the Form1 designer after rebuilding the WinForms project.
The result of running the project and clicking the Label in the UserControl is shown in the figure.
Related
I develop chat application on Xamarin.Forms.
I need to don't hide keyboard when I press Send button and hide keyboard when I tap anywhere else.
I made it for iOS.
Can I make the same for android?
Found a very simple method to achieve this goal, you can simply place a Button on the top of an Entry like this:
<Grid VerticalOptions="End">
<Entry x:Name="MessageEntry" TextChanged="MessageEntry_TextChanged_1" />
<Button x:Name="SendButton" Text="Send" HorizontalOptions="End" Clicked="Button_Clicked" IsEnabled="False" />
</Grid>
Code behind:
private void Button_Clicked(object sender, EventArgs e)
{
MessageEntry.Text = null;
}
private void MessageEntry_TextChanged_1(object sender, TextChangedEventArgs e)
{
if (MessageEntry.Text != null)
SendButton.IsEnabled = true;
else
SendButton.IsEnabled = false;
}
Tested on Android 6.0 emulator, works fine to me, you can customize the Button to make it looks more beautiful in this view:
you can have in your PCL a scrollview element in which you add you entry element, when you tap on the entry element it will bring the keyboard and not hide it, it only hide it when tapped on a different portion of the screen. let me know if it help
I have a Control that contains a Popup. I am trying to close the Popup whenever someone clicks outside of the Control. This is the part of my code that sets up the problem:
AddHandler(Mouse.PreviewMouseDownOutsideCapturedElementEvent, new MouseuttonEventHandler(HandleOutsideClick), true);
Now whenever I click in the Popup it causes PreviewMouseDownOutsideCapturedElementEvent to be raised. Am I misunderstanding this event? Is there something that I can do to have the Popup be considered a part of the Control so that it doesn't raise this event?
Does this work?
<Popup Name="Pop" LostFocus="ClosePop"/>
private void ClosePop(object sender, RoutedEventArgs e)
{
Pop.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
}
Put the XAML code in your .xaml page and the C# code in the related .xaml.cs file.
Note: You may need to put the focus on the popup before this works, it may be done automatically; I haven't done this on popups, but I have done it on other objects.
Update: This works for me, clicking in the TextBox that says Test1 opens the Popup, and clicking in the TextBox labeled Test2 closes it:
<Grid Background="White">
<StackPanel>
<TextBox Foreground="Black" LostFocus="ClosePop" GotFocus="OpenPop" Height="50">Test1</TextBox>
<TextBox Foreground="Black" Height="50">Test2</TextBox>
</StackPanel>
<Popup Name="Pop" Height="50" Width="50">
<TextBlock Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center">Pop!</TextBlock>
</Popup>
</Grid>
private void ClosePop(object sender, RoutedEventArgs e)
{
Pop.IsOpen = false;
}
private void OpenPop(object sender, RoutedEventArgs e)
{
Pop.IsOpen = true;
}
I have user control and wanna show it by clicking a button in a window designed by wpf.
I made a control user project and referenced it in a wpf project in this way:
xmlns:myproj="clr-namespace:WpfControlLibrary1;assembly=WpfControlLibrary1"
and in <Grid>tag I have this :
<myproj:UserControl1 Visibility="Hidden"
x:Name="customproj" />
and as I told I have a button in the main window in wpf project:
<Button Content="click me" HorizontalAlignment="Left" Margin="186,127,0,0" VerticalAlignment="Top" Width="124" Height="31" Click="Button_Click"/>
but I don't know how to write the event of the Button_Clickto open the control user.
private void Button_Click(object sender, RoutedEventArgs e)
{
//I don't know what to write!!!
}
I searched a lot but didn't find a suitable answer for my problem!
thank you
Just set the Visibility of UserControl to Visible:
private void Button_Click(object sender, RoutedEventArgs e)
{
customproj.Visibility = Visibility.Visible;
}
I'm trying to set up a WPF window so that it can accept different types of data via Drag and Drop. If I make a new project and set the window to the following:
<Window x:Class="DropShare.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" AllowDrop="True" DragEnter="Window_DragEnter">
<Grid>
</Grid>
</Window>
And set the code-behind to:
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void Window_DragEnter(object sender, DragEventArgs e)
{
}
}
I only ever get DragEnter firing for files. It never fires for anything else - text, images, etc.
Is there something I'm missing? All the tutorials I've read have seemed to suggest this is all that's needed as the DragEnter event handler let's me state what I accept.
So your code works fine for me. But try this...
In your Window:
<Label Background="Purple" HorizontalAlignment="Center" VerticalAlignment="Center" Content="Drag from here!" MouseDown="Label_MouseDown"/>
and in your code behind:
private void Label_MouseDown(object sender, MouseButtonEventArgs e)
{
DragDrop.DoDragDrop(this, "This is just a test", DragDropEffects.All);
}
Then drag from the label into the window and see if your event fires.
If this works, it may have something to do with the permissions level between Visual Studio and your outside environment (possibly).
See:
https://superuser.com/questions/59051/drag-and-drop-file-into-application-under-run-as-administrator
In WPF drag and drop feature always has to deal with DragDrop Class, Please check here how to do drag and drop across applications
I have a SWF object embedded in a WindowsFormsHost Control inside a WPF window.
I'd like to add a toolbar over the swf movie.
The problem with the snippet of code I have below, is that when the new child is added to the host control (or the movie is loaded, I haven't figured out which yet), the toolbar is effectively invisible. It seems like the z-index of the swf is for some reason set to the top.
Here is what it looks like:
XAML:
<Grid Name="Player">
<WindowsFormsHost Name="host" Panel.ZIndex="0" />
<Grid Name="toolbar" Panel.ZIndex="1" Height="50"
VerticalAlignment="Bottom">
[play, pause, seek columns go here]
</Grid>
</Grid>
C#:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
flash = new AxShockwaveFlashObjects.AxShockwaveFlash();
host.Child = flash;
flash.LoadMovie(0, [movie]); // Movie plays, but no toolbar :(
}
Any insight on this issue would be much appreciated.
Update: Since no suitable answer was posted, I've placed my own solution below. I realize this is more of a hack than a solution so I'm open to other suggestions.
Here is my hackaround the WindowsFormsHost Z-index issue.
The idea is to place whatever you need to be overlayed nested inside a Popup. Then to update that popup's position as per this answer whenever the window is resized/moved.
Note: You'll probably also want to handle events when the window becomes activated/deactivated, so the pop disappears when the window goes out of focus (or behind another window).
XAML:
<Window [stuff]
LocationChanged="Window_LocationChanged"
SizeChanged="Window_SizeChanged" >
<Grid Name="Player">
[same code as before]
<Popup Name="toolbar_popup" IsOpen="True" PlacementTarget="{Binding ElementName=host}">
[toolbar grid goes here]
</Popup>
</Grid>
</Window>
C#:
private void resetPopup()
{
// Update position
// https://stackoverflow.com/a/2466030/865883
var offset = toolbar_popup.HorizontalOffset;
toolbar_popup.HorizontalOffset = offset + 1;
toolbar_popup.HorizontalOffset = offset;
// Resizing
toolbar_popup.Width = Player.ActualWidth;
toolbar_popup.PlacementRectangle = new Rect(0, host.ActualHeight, 0, 0);
toolbar_popup.Placement = System.Windows.Controls.Primitives.PlacementMode.Top;
}
private void Window_LocationChanged(object sender, EventArgs e)
{ resetPopup(); }
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{ resetPopup(); }
Another solution I've discovered is to use Windows Forms' ElementHost control. Since I'm using a Windows Form inside a WPF window anyway, why not just use an entire Windows Form and save myself Z-Issue headaches.
The ElementHost control is really useful, because I can still use my toolbar UserControl, and embed it inside the Windows Form. I've discovered that adding a child can be finicky with Windows Forms, so here's a snippet describing the solution:
First, toss in the ActiveX object, then an ElementHost Control, using the designer.
Form1.Designer.cs:
private AxShockwaveFlashObjects.AxShockwaveFlash flash;
private System.Windows.Forms.Integration.ElementHost elementHost1;
Form1.cs
public Form1(string source)
{
InitializeComponent();
toolbar = new UserControl1();
this.elementHost1.Child = this.toolbar;
this.flash.LoadMovie(0, source);
}
Note that the child was not set in the designer. I found that for more complex UserControls the designer will complain (though nothing happens at runtime).
This solution is, of course, still not entirely ideal, but it provides the best of both worlds: I can still code my UserControls in XAML, but now I don't have to worry about Z-indexing issues.