I'm starting with WPF but I can't find the way to inherit from a window to another window.
Now, I'm starting a new application on WinForm in which I created a generic window to add/edit/remove/consult objects. So the windows have some buttons, a header, a footer, a background color, etc.
Then, I inherit this windows to the windows for each object.
So without doing anything, I have amost the full page ready.
My question is... Can I do the same with WPF? Because, I want to stop doing the app with Winform and start working with WPF.
I spend a lot of time looking for how to do this in WPF, but I can't find the way. I saw that WPF can't inherit the appearance.
I saw custom controls or user controls, but I need to put things inside the control that I inherit to complete the screen.
Yes, it is possible. You need to declare your new class as public (actually this to ensure both classes have the same reach)
public class newWindow : Window
Then, inherit from your new class
public partial class TestClass : newWindow
Then, modify the TestClass xaml file:
<base:newWindow>
xmlns:local="clr-namespace:YourNameSpace"
xmlns:base="clr-namespace:YourNameSpace"
</base:newWindow>
WPF does not allow you to visually inherit in this way.
What I would suggest doing is creating a custom class that inherits from Window, and add RelayCommand properties for the different buttons you want to display on all windows. Have the RelayCommands call Virtual methods that perform the necessary logic.
Then, create a custom ControlTemplate (make it the default so any custom window will use this tempalte) for your custom Window which includes a Header or Footer section that has the buttons you want to include on all windows. You can then DataBind these buttons to the Commands in your custom window class (perhaps through a RelativeSource Self binding).
This approach should allow you to create windows that have a "shared" footer, but different content. If you're fairly new to WPF, this might be a little tricky. Another approach would be to create a UserControl that implements the Header/Footer functionality and raises events for the button actions, then just drop it on all your windows. But this approach fails to leverage the power of WPF, specifically the lookless nature of controls, in my opinion.
Related
I need to develop an multipage WPF application. I tried to achieve that by using one Window and multiple Pages. When I need to switch from one to another page I just change Content of main Window to point to that page. I have filling that I'm using wrong scenario because I don't have idea how to reuse XAML code for example an Menu controll, StatusBar and ToolBar for all pages.
You need ContentControl. It changes view based on ViewModel. Place your static elements in one place and in changeable area a ContenControl object.
More here and here.
One idea is you have a PageViewModelBase that implement a base virtual Property and Methods for Menu and StatusBar model and ... Then Each PageViewModel Override proper implementation of PageViewModelBase, Then Window contains Menu or StatusBar that Bind to DataContext of Pages.
I have a WPF application. I'm trying to modify it to use the MVVM pattern.
Now, in the code behind of my main window, when I click a button, I change the "Content" of my window with a WebBrowser control :
Content = webBrowserWrapper.WebBrowser;
I would like to do it from a command in my ViewModel. How can I access the "Content" property of my window ?
If it's not possible, what is the simple way to modify the content of my window (maybe create a new window and navigate to it)
The principal behind MVVM is that UI is the concern of the View, and logic is the concern of the ViewModel.
You describe a concern of the View, which is properly handled within its codebehind. There is no need to involve the ViewModel in this action.
So, in other words... congrats. You're already there.
I have a MainWindow in my WPF app.
This MainWindow has a menu on the left and when a menu option is selected a UserControl is loaded on the right.
This is similar to Master Pages in asp.net
What I want to do now is to have a modal window show from the UserControl which will only allow the user to interact with the modal window.
I have seen examples of the Main Window showing a modal window (http://www.codeproject.com/Articles/36516/WPF-Modal-Dialog) but not sure on how to load this from a UserControl.
There's this: dialogs and mvvm but this is the best example I've seen of dealing with it: mvvm and closing forms
The first link I've not used and stumbled across while looking for the second link to post that here. The second link has two downloads, you can ignore the _service download, it's basically the same.
One way in WPF is this method
Add a new Window to the project.
Add other controls onto the window as needed.
In XAML name the window such as x:Name="MyWindow"
Put on Dependency properties on the window and have each of the controls bind to the window's data context such as {Binding MyText, ElementName=MyWindow}. (Note I still use, even for WPF these Visual Studio code snippets to add different dependency properties, these save time for a very redundant operations of adding them: Silverlight Snippets.
In the location where you want to launch the model dialog use this example:
Example:
var about = new About(); // Create the new window
// I've added a CompanyName dependency property.
about.CompanyName = "OmegaMan Industries";
about.ShowDialog();
I want to create custom WPF control that has a single "child" control inside. Subclassing ContentControl or UserControl works, but has one flaw: these controls don't work in designer mode.
By "don't work" I mean this scenario: suppose I have a Canvas with my custom control in it. I want to put, say, a Button inside my control. I drag it from the toolbox, and it appears inside my control. However, XAML view shows that the new button actually belongs to Canvas, not to my control.
I can place it inside my control by manually editing XAML, but I want the designer to work too.
Interestingly, when I subclass Canvas, Grid or Panel, designer works as expected. However, these controls have many children, which is not what I need.
How can I make a single-child control that works in designer?
how about inheriting from Border? that way you could spare yourself the hassle with Designer Extensibility
I had the same problem with a content control I am writing and found an easy solution on this
StackOverflow thread.
Just implement the HitTestCore method:
protected override System.Windows.Media.HitTestResult HitTestCore(System.Windows.Media.PointHitTestParameters hitTestParameters)
{
return new PointHitTestResult(this, hitTestParameters.HitPoint);
}
I also had a similar question here.
But after digging and digging it seams that the real answer is "NO", there isn't any official way to support dragging controls straight into a custom Content-Control at Design-Time, even implementing 'HitTestCore' as Stephan's answer suggests, does not enable drag&drop at design-time for ContentControl.
For such purposes you should consider inheriting from either Grid or Panel (to allow multiple child controls), or Border (to allow single child).
There are many articles that described how to create a custom shaped window in WPF but none of them (at least I can't find any) described how can build a reusable one such as inheriting from it in other windows.
I have tried to create a ControlTemplate from my custom window. The problem is with the Close button and the MoveDrag() method.
If I wire the event to one of my other controls in ControlTemplate their Parent property is null, so I can not drag or close the window.
Does anyone have any ideas how can I create a reusable custom window template or something?
Unfortunately there is no such things as visual inheritance in WPF. (no xaml inheritance to be more specific)
For your specific issue, instead of inheriting, you could create a CustomForm as a template (with a big empty container in the middle), and then create all your other forms as usercontrols that fill that container.
The following will return the window object containing the control:
Window.GetWindow(myControl)