I'm using the WPF Ribbon Application Menu:
https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=11877
https://msdn.microsoft.com/de-de/library/ff799534(v=vs.110).aspx
How can I close the Application (File) Menu programmatically?
How can I detect if the user opens the Application Menu? I didn't found an appropriated event
You need IsDropDownOpen property and related event(s).
XAML (this is for .NET 4.5+, but for 4.0 it will be almost the same, the difference will be in namespace prefix):
<StackPanel>
<Ribbon>
<Ribbon.ApplicationMenu>
<RibbonApplicationMenu x:Name="Menu" DropDownOpened="RibbonApplicationMenu_DropDownOpened">
<RibbonApplicationMenuItem Header="Foo"/>
<RibbonApplicationMenuItem Header="Bar"/>
</RibbonApplicationMenu>
</Ribbon.ApplicationMenu>
</Ribbon>
</StackPanel>
Code-behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void RibbonApplicationMenu_DropDownOpened(object sender, EventArgs e)
{
// user has opened menu
Debug.WriteLine("Menu opened.");
// let's close it from code
Menu.IsDropDownOpen = false;
}
}
Also, you may want to disable entire menu. This can be done using IsEnabled property.
Related
I found the problem is It cannot close this window. But it can open the MainWindow.
pls help
Code in button
private void LoginBtn_Click(object sender, RoutedEventArgs e)
{
MainWindow MainView = new MainWindow();
MainView.Show();
AuthWindow AuthView = new AuthWindow();
AuthView.Close();
}
I want to press the button inside the page and close that window and open another window.
For such scenarios, I advise you to use RoutedCommand. In this case, you can use the ready-made command ApplicationCommands.Close.
In the page button, specify the command name:
<Button Content="Close Window" Command="Close" />
In the Window, set command executing:
<Window.CommandBindings>
<CommandBinding Command="Close" Executed="OnCloseWindow"/>
</Window.CommandBindings>
<x:Code>
<![CDATA[
private void OnCloseWindow(object sender, ExecutedRoutedEventArgs e)
{
this.Close();
}
]]>
</x:Code>
P.S. I also do not advise you to open new windows. Since you are using Pages, you should change Pages in a single Window. And closing the Window is regarded as an Exit from the Application.
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.
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 want to be able to show a preview of a screen saver inside WPF window. (using a container or control or ...) I know that Windows itself passes "/p" argument to the screen saver to get a preview. But how can I show that preview inside my WPF application? Should I get a handle of it and change its parent to my container o control? How?
You need to use Windows.Forms interop, because screen savers expect windows handles (HWND) and in WPF, only top-level windows have them.
MainWindow.xaml
<Window x:Class="So18547663WpfScreenSaverPreview.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:forms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="Screen Saver Preview" Height="350" Width="525"
Loaded="MainWindow_OnLoaded" Closed="MainWindow_OnClosed"
SizeToContent="WidthAndHeight">
<StackPanel Orientation="Vertical" Margin="8">
<TextBlock Text="Preview"/>
<WindowsFormsHost x:Name="host" Width="320" Height="240">
<forms:Control Width="320" Height="240"/>
</WindowsFormsHost>
</StackPanel>
</Window>
MainWindow.xaml.cs
using System;
using System.Diagnostics;
using System.Windows;
namespace So18547663WpfScreenSaverPreview
{
public partial class MainWindow
{
private Process saver;
public MainWindow ()
{
InitializeComponent();
}
private void MainWindow_OnLoaded (object sender, RoutedEventArgs e)
{
saver = Process.Start(new ProcessStartInfo {
FileName = "Bubbles.scr",
Arguments = "/p " + host.Child.Handle,
UseShellExecute = false,
});
}
private void MainWindow_OnClosed (object sender, EventArgs e)
{
// Optional. Screen savers should close themselves
// when the parent window is destroyed.
saver.Kill();
}
}
}
Assembly references
WindowsFormsIntegration
System.Windows.Forms
Related links
Walkthrough: Hosting a Windows Forms Control in WPF
Creating a Screen Saver with C# (describes command line arguments)
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