WPF using a view to change the parents atributes - wpf

My program has several views that are controlled using a Hamburger menu. Inside one of these views i have made a toggle switch to change between fullscreen and windowed. The only problem is that to do this i need to change the Mainwindow.xaml from within the view. The mainwindow is a "MetroWindow"
I have tried adapting the following:
Window parent = Window.GetWindow(this);
This works fine but when i want to use a metro specific attribute:
Parent.IgnoreTaskbarOnMaximize = true;
It obviously doesn't contain a reference as its not a window its a metrowindow. I changed the code to a metrowindow but that wont work as it cant convert from windows.window to mahapps.metro.controls.metrowindow.
MetroWindow metroParent = MetroWindow.GetWindow(this);
Is there anyway i can do this or run code from the mainwindow code within my view ?

Cast the result:
MetroWindow parent = Window.GetWindow(this) as MetroWindow;

Related

Lightswitch modal window

I've got standard CreateNewEntity screen. Entity can contain list of entities of some other type. By default there is an add button that opens modal window when user wants to add another entity into collection. However, default modal window was lacking some of the needed functionality so I've done a bit of research. Turns out that default modal screens cannot be modified. So, I found a nice custom modal window helper class. The problem is that I can't seem to be able to access modal window fields in order to enforce needed logic. There are two dropdown lists that are associated. Change in one will result in limiting the other dropdown list options. I'm stuck at this particular part:
var proxy = this.FindControl("DodavanjeParcele");
proxy.ControlAvailable += (s, e) =>
{
var ctrl = e.Control as System.Windows.Controls.Control;
//how to obtain access to ctrl fields?
};
"DodavanjeParcele" is custom modal window. Before this, modal window is instantiated and initialized. It pops up after button click and functions as expected. The only thing missing are above-mentioned rules. I need to set change event handlers for modal window fields in order to define rules. As seen above I tried to cast IProxy as a standard Windows control. This is where I got stuck. I can't seem to find a way to access control fields and set event handlers. Any thoughts?
If I understand you correctly, I'm not sure why you need to search through controls or cast anything.
Control1 is an entity which creates an AutoComplete Box (dropdown list). That selection is copied into a local property in the Control1_Changed method. That property is used as a parameter in a filter query to create Control2.
C#:
private void Control1_Changed()
{
this.MyLocalProperty = this.Control1.SelectedItem;
}
VB.NET:
Private Sub Control1_Changed()
Me.MyLocalProperty = Me.Control1.SelectedItem
End Sub
Just make sure you have Auto Execute Query checked in Control2's Properties and the second control should update and filter when Control1 changes the query parameter.
The code in my screen shots all takes place inside of Yann's Modal Helper so there is nothing special you need to do.

Navigate between views WPF PRISM application

I am working on a WPF PRISM application that has the following structure (I have simplified to explain better without additional layers). I am using Unity as my DI/IOC
AppMain - Bootstrapper
Gui - Views and View Models
Data - Data using EF.
In Gui, I have views names as below:
Home
EmployeesView
OrdersView
Reports
I have three regions in the shell.
MainRegion - Main Content
TopRegion - Navigation Menu
BottomRegion - Status Bar
I am using the following method to register views to the regions
IRegion region = _regionManger.Regions[RegionNames.MainRegion];
var mainView = _container.Resolve<Home>();
region.Add(mainView, ViewNames.HomeViewName);
region.Activate(mainView);
The first of activation happens in the Module Initialize method for Top, Main and Bottom.
After this, I am activating other views when the button are clicked. It is just code behind for now. Sample code here:
IRegion region = _regionManger.Regions[RegionNames.MainRegion];
var reportView = region.GetView(ViewNames.ReportsViewName);
if (reportView == null)
{
reportView = _container.Resolve<ReportsView>();
region.Add(reportView, ViewNames.ReportsViewName);
region.Activate(reportView);
}
else
{
region.RequestNavigate(ViewNames.ReportsViewName);
}
PROBLEM1: Any advise on how this can be done or the way I am doing is fine.
The top menu has Home, Employees, Orders, Reports buttons.
In the home page I have recent orders by the employee in datagrid as readonly.
I would like to double click to navigate to the OrderView and pass the selected order to show to the user. PROBLEM2 I am not sure where to do the navigation for this.
PROBLEM3: Another issue was if set the RegionMemberLifeTime keepAlive false, INavigationAware methods don't fire. If I don't set the KeepAlive to false, the page does not get refreshed because, the view model does not get called.
I need the pages to refresh when it is navigated to and not be stale and also handle any confirm prompts to the user when the view is navigated away from it.
Your help is very much appreciated.
it's certainly too late but…
Problem 1/2: is there a particular reason why you add content to region in module initializer?
the common way is more like -> in xaml:
<ContentControl prism:RegionManager.RegionName="MainRegion" />
and in ModuleInit.cs -> Initialize()
_regionManager.RegisterViewWithRegion("MainRegion", () => _container.Resolve<MainView>());
Problem 3:
the view has to implements INavigationAware, IRegionMemberLifetime
and to swich region, in the viewModel you do:
_regionManager.RequestNavigate("RegionWhatever", new Uri("TestView", UriKind.Relative));
But to work you have to register it in ModulInit.cs as an object with viewName, like that:
_container.RegisterType<Object, TestView>("TestView");
and a contentControl with the correct RegionName define in xaml of course

Displaying Page Over MainWindow in WPF

I have a main window in Which I have a button. When i click that button i want to show a dialog box which i have made using a page. I am using
Page1 P = new Page(); //Page which i want to show over my main window
this.P.Visibility = Visibility.Visible;
when i used Window to make dialog box it is working fine but with pages there is no Show property associated. Does anyone know Of any method to display page over Mainwindow.
A Page cannot be shown as a window. For this you need to use Window, both can host the same XAML content, you just need to use the right container!
See this question:
Page vs Window in WPF?

In my Windows Phone app,I want to turn my current page unable in gray and show a textbox in foreground

Like the title said,how may i turn the grid(or page) and the components in it into background gray and unable and show a new component in foreground.
It's a common effect in the web page,but i cannot do that with a xaml.
Please answer in detail better with sample code if you do.Thanks in advance.
Here's an example of using a Popup to display a UserControl when a Page is navigated to. The OnNavigatedTo function creates a popup, binds a Click event to the UserControl, and then shows the UserControl over the entire page. If the correct password is entered, ("password" in the example) the control disappears. You'll need to modify it for your own requirements, but it should definitely get you started.
https://skydrive.live.com/redir.aspx?cid=ef08824b672fb5d8&resid=EF08824B672FB5D8!343
You'll want to use the VisualStateManager and create some animation transitions to switch between the old and new components. I won't paste the code here for brevity, but if you check out:
http://visualstudiomagazine.com/articles/2011/07/22/wcmob_visual-states.aspx
You'll see a simple example of switching between two UI components on a single page within a Windows Phone 7 PhoneApplicationPage. In your scenario, you'd use this paradigm to create 2 animation storyboards, one for showing an overlay control and one for settings the 'disabled' property on the main grid.

How to inherit from silverlight Button?

I need to have button have 4 images that are changed by the application action.
I want to define new class ButtonEx that inherit from the Button class.
So, i did it by adding 'silverlight template control' and it to be inherit from control to Button.
Now, i cant see the images whan i put this ButtonEx on the page.
What i did wrong ?
How to fix it ?
( same code work fine in WPF )
Can you post any of your code? Did you create a visual style for it in themes\generic.xaml? Did you set the default style key?
I recently followed this tutuorial for a TextBox, you could so something similar for your button... http://www.silverlightshow.net/items/Create-a-Custom-Control-Inheriting-from-TextBox.aspx
Store your images as resources then create a user control that has
- a button and the content of the button is an image
- a property to select the image from the resources
use the usercontrol as your button.
please post a code sample...
to inherit form button you simply need this.
public class ButtonEx:Button
{
}
this will have all functionality that button has.
if you want new template just add it in Themes/generic.xaml
From your question its not clear that you really need to be subclassing Button. Maybe you can get by with just copying and modifying the template of button (Blend helps for this), or just have the image as content bound to some property for the application to change (using a ValueConverter perhaps).

Resources