An WPF project has a MainWindow holding multiple books (in this case just one).
The pencil icon is in fact a button creating a new instance of the BookEdit window and showing it. Clicking it should show the following (and it used to do so).
My issue is that recently, the secondary window starts snapshotting the mainwindow and showing a portion of it as part of the window:
and it keeps doing so even when I move the window:
Why does that happen? Is it an issue with my GPU/whatever? I cannot currently test the software on another device.
I also removed any ZIndex from the solution.
Edit 1:
It should be noted that the books from MainWindow are generated behind using Children.Add
Button newbutt = new Button();
newbutt.Background = null;
newbutt.SetResourceReference(Control.StyleProperty, "BookToolbarButton");
newbutt.Click += new RoutedEventHandler(FileHandler.editBook);
Image pencil = new Image();
pencil.Source = new BitmapImage(new Uri("pack://application:,,,/Images/pencil.png"));
newbutt.Content = pencil;
sp.Children.Add(newbutt);
editBook():
string name = (((sender as Button).Parent as StackPanel).Parent as Grid).Name;
if (System.String.IsNullOrEmpty(name)) Console.WriteLine("Error: The book does not hold a ");
else
{
Console.WriteLine("Editting book {0}", name);
BookEdit bEdit = new BookEdit("books/" + name + ".json");
bEdit.Show();
}
Related
I want to open a WPF ContextMenu when the user clicks the system tray icon. With Windows Forms this is straight-forward, just call notifyIcon.ContextMenu = contextMenu and you're set.
On WPF we can not set the ContextMenu that easily because WPF's ContextMenu class is not related to Forms ContextMenu. The alternative I have been pursuing is to handle NotifyIcon's Click event to open the WPF-style ContextMenu.
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
// This is intended to be a system tray application so we hide the window
this.Visibility = Visibility.Hidden;
// Winform context menu
// The context menu closes when the user clicks anywhere outside the menu
// The user can navigate the menu with the keyboard arrows and close with ESC
var notifyIcon1 = new System.Windows.Forms.NotifyIcon();
var contextMenu = new System.Windows.Forms.ContextMenu();
var menuItem = new System.Windows.Forms.MenuItem();
menuItem.Text = "WinForm Menu Item";
contextMenu.MenuItems.Add(menuItem);
notifyIcon1.ContextMenu = contextMenu;
notifyIcon1.Icon = Properties.Resources.ico;
notifyIcon1.Visible = true;
// WPF context menu
// The user cannot close the menu by clicking outside its bounds
// Does not detect any keyboard input
var notifyIcon2 = new System.Windows.Forms.NotifyIcon();
notifyIcon2.Icon = Properties.Resources.ico;
notifyIcon2.Visible = true;
notifyIcon2.Click += NotifyIcon2_Click;
}
private void NotifyIcon2_Click(object sender, EventArgs e)
{
var contextMenu = new ContextMenu();
var menuItem = new MenuItem();
menuItem.Header = "WPF Menu Item";
contextMenu.Items.Add(menuItem);
contextMenu.IsOpen = true;
}
}
The issue with this approach is that the WPF ContextMenu never gets any hint that the user has navigated away from the menu and should close (Eg, when the user clicks outside the bounds of the menu). None of the Focus or MouseCapture events are ever triggered and I am unable to close the menu other than by clicking on one of its items.
So the question here, to put slightly different, is: how do I properly emulate the NotifyIcon's ContextMenu closing behavior using WPF's ContextMenu?
I faced similar issue. You can try if you want
notifyIcon1.ContextMenuStrip = new Forms.ContextMenuStrip();
notifyIcon1.ContextMenuStrip.Items.Add("YourMenuItem",null, MenuItemEvent);
I hope this will solve the problem.
I have a C# Form application that has a TabControl in the main form. This TabControl is used to display multilple TabPages that contain a CustomControl. This CustomControl is just a Panel with a few buttons and a PictureBox.
Here is a picture of my app when it starts up. As you can see the tab control (the white area) is empty:
If the user clicks the "Add Image" button they are presented with an OpenFileDialog to select the image then the addImage method is called with the selected file:
private void doAddImage()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = Constants.Global.IMAGE_FILE_FILTER();
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string imageFileName = openFileDialog.FileName;
addImage(imageFileName);
}
}
private void addImage(string imageFileName)
{
// Create a new bitmap and image preview custom control. Then create a new tab
// page and add the custom control to the tab page.
Bitmap bitMap = new Bitmap(imageFileName);
ImagePreviewCustomControl previewControl = new ImagePreviewCustomControl(bitMap);
previewControl.Dock = DockStyle.Fill;
TabPage tabPage = new TabPage(Path.GetFileNameWithoutExtension(imageFileName));
tabPage.Controls.Add(previewControl);
// Insert the new tab page to the right of the currently selected tab page
int selectedTabIndex = imagesTabControl.SelectedIndex;
imagesTabControl.TabPages.Insert(selectedTabIndex + 1, tabPage);
imagesTabControl.SelectedIndex = selectedTabIndex + 1;
}
As you can see, in the addImage method I create the Bitmap, CustomControl, and TabPage and then insert it into the TabControl.
I start my application, click the "Add Image" button, everything works just fine.
Here is a picture with a tab page added:
While I am testing my app I don't want to have to add an image manually using the OpenFileDialog every time so in my constructor I just call addImage with some fixed image file name that I want to test with:
public ImageViewerApp()
{
InitializeComponent();
addImage(#"C:\MyImages\Calculator-3-icon.png");
}
The problem I am having is that when I try to add the image in my constructor it doesn't show up in the TabControl. The application starts up blank (like the first picture).
As stated above when the application is already running and I click the "Add Image" button it gets added just fine.
I found a property in the TabControl class called Created which states:
"Gets a value indicating whether the control has been created"
So to try and figure out what's going on I write the value of Created to the console just before I call addImage in the constructor. (I have a custom console for debugging my Form applications.)
public ImageViewerApp()
{
InitializeComponent();
TestConsole.WriteLine(imagesTabControl.Created);
addImage(#"D:\Development\Work\Other\Stardock\Start8\_downloaded\Calculator-3-icon.png");
}
The value of Created just before the call to addImage in the constructor is:
False
I put another console output inside the addImage method:
private void doAddImage()
{
TestConsole.WriteLine(imagesTabControl.Created);
OpenFileDialog openFileDialog = new OpenFileDialog();
...
...
}
The value of Created after the app has started and the user presses the "Add Image" button is:
True
Why is it that the TabControl is not Created inside my constructor (even after the InitializeComponent() call) and the once the application is running it is Created?
=UPDATE========================================================================
Based on the suggestion by Hans Passant I have added the following code to my addImage method:
int selectedTabIndex = -1;
if (imagesTabControl.TabCount > 0)
{
selectedTabIndex = imagesTabControl.SelectedIndex;
}
else
{
selectedTabIndex = imagesTabControl.SelectedIndex + 1;
}
imagesTabControl.TabPages.Insert(selectedTabIndex, tabPage);
imagesTabControl.SelectedIndex = selectedTabIndex;
This doesn't work.
===============================================================================
=UPDATE2=======================================================================
int selectedTabIndex = imagesTabControl.SelectedIndex;
if (imagesTabControl.TabCount == 0) selectedTabIndex = -1;
imagesTabControl.TabPages.Insert(selectedTabIndex, tabPage);
imagesTabControl.SelectedIndex = selectedTabIndex;
This causes the following Exception:
{"InvalidArgument=Value of '-1' is not valid for 'index'.\r\nParameter name: index"}
===============================================================================
=UPDATE3=======================================================================
I tried the folllowing code:
int selectedTabIndex = imagesTabControl.SelectedIndex;
if (imagesTabControl.TabCount == 0) selectedTabIndex = -1;
imagesTabControl.TabPages.Insert(selectedTabIndex + 1, tabPage);
imagesTabControl.SelectedIndex = selectedTabIndex + 1;
This one doesn't throw an exception but again no tab page added after calling
addImage in the constructor.
===============================================================================
=UPDATE4=======================================================================
I have kindof given up on adding an image in the constructor. So instead I am using an enum RunMode and a variable RUN_MODE of that type. Then, if RUN_MODE == RunMode.TESTI call a method to add a random image when I click the button. (The OpenFileDialog is not used. I just parse through all the image files in the fixed directory IMAGE_DIRECTORY.
enum RunMode { NORMAL, TEST }
private static string IMAGE_DIRECTORY = #"D:\\Work\Images";
...
...
private void doAddImage()
{
if (RUN_MODE == RunMode.TEST)
{
addRandomImage();
return;
}
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = Constants.Global.IMAGE_FILE_FILTER();
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string imageFileName = openFileDialog.FileName;
addImage(imageFileName);
}
}
private void addRandomImage()
{
string[] allFiles = Directory.GetFiles(IMAGE_DIRECTORY);
List<string> imageFileNames = new List<string>();
foreach (string file in allFiles)
{
bool isImageFile = Constants.Global.IMAGE_EXTENSIONS.Contains(Path.GetExtension(file));
if (isImageFile)
{
imageFileNames.Add(file);
}
}
int randomIndex = new Random().Next(imageFileNames.Count);
addImage(imageFileNames.ElementAt(randomIndex));
}
This works. Now when I click the "Add Image" button during TEST_MODE I skip the
OpenFileDialog and just add a random image.
I would like to understand the issues with TabControl but at this point I just
need to continue development. My current solution works great.
As I person who like to understand everything I would like to use other people's
suggestions so I will keep monitoring this question for a solution.
===============================================================================
JonP's answer gave me the idea to just wait for the window handle to be created before inserting the tab, so I tried some events occuring between Form construction and Tab Control display.
I found it to work with both the Load or Shown events:
Right-click on the Form (the root, not child controls) in the Designer view > Properties > Events (flash icon) > Behavior > enter a method name for the Load or Shown event and confirm. To generate a Load event callback you can also double-click on the Form itself. This should generate something like this:
this.Load += new System.EventHandler(this.Form1_Load);
// or
this.Shown += new System.EventHandler(this.Form1_Shown);
Setup the tabs in the callback:
private void Form1_Load(object sender, EventArgs e)
{
// Add image (this will call imagesTabControl.TabPages.Insert(selectedTabIndex + 1, tabPage))
// This must be done on Load event because Insert requires
// the window handle, which is not ready in the constructor
addImage(#"path_to_image.png");
}
I have had this problem too and have found a workaround; I think it must be a bug with Insert():
Don't use Insert(), it usually does nothing, use Add() instead; this reliably adds a TabPage to the end of the collection.
After adding it swap it with the tab position where you actually want it.
imagesTabControl.TabPages.Add(tabPage);
// Now swap the two tabs:
imagesTabControl.TabPages[imagesTabControl.TabCount - 1] = imagesTabControl.TabPages[selectedTabIndex + 1];
imagesTabControl.TabPage[selectedTabIndex + 1] = tabPage;
Your mileage may vary of course :-)
Stop Press! An even better fix is to read the class's Handle member before calling Insert():
var handle = imagesTabControl.Handle;
Insert() works perfectly after you do that. Obvious isn't it???? The help page for Handle has this possible relevant Remark showing that the object actually does something when you read Handle:
The value of the Handle property is a Windows HWND. If the handle has not yet been created, referencing this property will force the handle to be created.
You could remove the TabControl from the designer and then instead just manually create the TabControl programmatically and add it to the Form immediately after InitializeComponent(). Then after you create the TabControl, call addImage(). Something like:
InitializeComponent();
TabControl tc = new TabControl();
tc.Location = new Point(10, 10);
tc.Size = new Size(100, 100);
tc.Visible = true;
tc.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Top);
this.Controls.Add(tc)
addImage("c:\pathToImage\image.bmp");
I wrote a simple wpf application which handles a TCPIP and a Serial communication. At the first version of the App I used a "GUI" with a connect button and so on. This version was running on the Tablet. The Tablet is a ThinkPad with Windows 8.
The second version the app should run without any GUI, but with an taskbar icon. I was able to create the taskbar icon and so. The app perfectly works on my computer. Its possible to install the app on the tablet, but it just doesn't run.
Has anybody a clue why the app doesn't open?
Beneath you can find some source code, how I created the icon and so on. In the Visual Studio project I disabled the "Main Window" which looks like this
xml-code
Title="MainWindow" Height="350" Width="525" Visibility="Hidden">
c# - code
public MainWindow()
{
InitializeComponent();
// Initailize Menu
this.components = new System.ComponentModel.Container();
this.contextMenu1 = new System.Windows.Forms.ContextMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
// Initialize contextMenu1
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem1 });
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem2 });
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem3 });
this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItem4 });
// Initialize Menu Items
this.menuItem3.Index = 0;
this.menuItem3.Text = "Connect";
this.menuItem3.Click += new System.EventHandler(this.Connect);
//
this.menuItem2.Index = 1;
this.menuItem2.Text = "Disconnect";
this.menuItem2.Click += new System.EventHandler(this.Disconnect);
//
this.menuItem4.Index = 2;
this.menuItem4.Text = "About";
this.menuItem4.Click += new System.EventHandler(this.About);
//
this.menuItem1.Index = 3;
this.menuItem1.Text = "Exit";
this.menuItem1.Click += new System.EventHandler(this.Exit);
// Create the NotifyIcon.
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
// The Icon property sets the icon that will appear
// in the systray for this application.
notifyIcon1.Icon = new Icon("icon.ico");
// The ContextMenu property sets the menu that will
// appear when the systray icon is right clicked.
notifyIcon1.ContextMenu = this.contextMenu1;
// The Text property sets the text that will be displayed,
// in a tooltip, when the mouse hovers over the systray icon.
notifyIcon1.Text = "Icon Test";
notifyIcon1.Visible = true;
//Handle the Click event
notifyIcon1.MouseUp += new MouseEventHandler(ShowContextMenu);
}
EDIT
is it possible that System.Windows.Input using directive is needed that the app starts on the tablet? I had to exclude that one because of a collision with another using directive..
There is another issue. I believe its an Windows 8 error. The app is working on my Win7 machine, but neither on a Win8 notebook nor a Win8 tablet.
I have a program that has some buttons, one of them is used to switch "Themes".
There are two themes, one is the normal Windows theme and the other is called Style2.
This is how I tried the switching
private bool UsingWindowsStyle = true;
private ResourceDictionary Style2= new ResourceDictionary() { Source = new Uri("/...;component/Resources/DefaultStyles.xaml", UriKind.RelativeOrAbsolute) };
private void SwitchButton_Click(object sender, RoutedEventArgs e)
{
if (UsingWindowsStyle)
{
Resources.MergedDictionaries.Add(Style2);
UsingWindowsStyle = false;
}
else
{
Resources.MergedDictionaries.Remove(Style2);
UsingWindowsStyle = true;
}
}
My problem is, when I use this program, and press this Button, this is what happens:
Window Opened Program operating normally with Windows theme.
SwitchButton First Click Program changes visuals to the Style2 theme. All the program's buttons operating normally.
SwitchButton Second Click Program reverts back to Windows theme, but all the buttons in the program seize to work.
Points to Consider
The program does not throw any exceptions at this point.
Debugging the code, it seems that after the second click, the program does not enter the SwitchButton_Click method.
I tried readding the Click EventHandler but with no use.
SwitchButton.Click += new RoutedEventHandler(SwitchButton_Click);
Thanks in advance for your help.
I would suggest that you are trying too hard. All you need to do is to change the Style on the Window itself. Leave the dictionaries alone. :-)
Here is an example that changes a windows style when you click from the list of available styles.
My command boils down to
//Here I am changing the style on the window
NewWindow.Style = ((StyleDetailsViewModel)x).Style;
NewWindow.Show();
with various input data
public StylingViewModel(Func<string, Style> findStyle)
{
Styles = new StyleDetailsViewModel[]
{
new StyleDetailsViewModel
{
Name = "None",
Description = "Completely remove all styling and show the raw NavigationWindow including default navigation elements",
WindowStyleNone = false,
Image = "\\Resources\\WindowStyleNone.png"
},
new StyleDetailsViewModel
{
Name = "PlainWindow",
Style = findStyle("PlainWindow"),
Description = "Hides the navigation elemetns of the NavigationWindow to make it look just like a normal window",
WindowStyleNone = false,
Image = "\\Resources\\WindowStylePlain.png"
},
new StyleDetailsViewModel
{
Name = "Windows 7",
Style = findStyle("Win7NavigationWindow"),
Description = "Uses glass effects to create a window that looks almost identical to the control panel from Windows 7.",
WindowStyleNone = false,
Image = "\\Resources\\WindowStyleWin7Nav.png"
},
and
this.DataContext = new StylingViewModel(x => (Style)this.FindResource(x));
Also beware of certain Window properties that can only be set before the window opens, such as WindowStyle="None" which you need if you are doing custom chrome.
I have a MVVM application and I have a main viewmodel. In this model I use the following code to open a dialog:
dlgViewModel myDlgViewModel = new dlgViewModel();
dlgView myDlgView = new dlgView();
myDlgView.DataContext = myDlgViewModel;
myDlgView.Owner = App.Current.MainWindow;
myDlgView.WindowStartupLocation = WindowStartupLocation.CenterOwner;
myDlgView.ShowDialog();
Also, in the XAML on my dlgView, I set the WIndowsStartupLocation to CenterOwner. However the window does not open in the center of the owner. I tried CenterScreeen but that does not work either.
How can I open a new view using the centerOwner or centerScreen option?
Well, the first time thing is set in the axml of my dialog WindowsStartUpLocation to center owner.
Then, in the main view model, I use this code:
dlgViewModel myDlgViewModel = new dlgViewModel();
dlgView myDlgView = new dlgView();
myDlgView.DataContext = miDlgViewModel;
myDlgView.Owner = App.Current.MainWindow;
myDlgView.ShowDialog();