Using Tabcontrol in MVVM pattern in WPF - c#

I have multiple window files and i want to merge my Xaml files(window) into a Tab control in a MVVM Pattern.
Each item Tab will represents a Xaml file.
i need something like this:
<TabControl >
<TabItem>
<local:FirstView></local:FirstView>
</TabItem>
<TabItem>
<local:SecondView></local:SecondView>
</TabItem>
</TabControl>
but i get this Error:
"Window must be the root of the tree. Cannot add Window as a child of Visual."
I have seen many topics like this but they use user control or they use a single view with multiple View Model.
Is there any way to import window(xaml) into tab control?
And another important thing, i want to have a button like Cancel, Pushing Cancel means we have to go back one level(go to another tab Item).
view model is not aware of view, so how can i navigate through them?

Is there any way to import window(xaml) into tab control?
No, there isn't. A System.Windows.Window cannot be a child of another System.Windows.Window.
The contents of the tab items should be defined as UserControls.
You should just be able to move the contents of your windows to the user controls.

Related

WPF Dynamic view of a dialog - contentpresenter cannot rendered

Based on this mechanism, I created a dialog window of which I can dynamically assign the content by a <ContentPresenter Content="{Binding .}">
The content I want to assign is an user control with a corresponding ViewModel. This works as I can render the DialogView in other usercontrols
<DataTemplate DataType="{x:Type ViewModels:DialogViewModel}">
<Views:DialogView/>
</DataTemplate>
)
However, in the DialogWindow, DialogView cannot be rendered but instead, only the string representation of DialogViewModel is visible. What might be the reason why I cannot render the view of contentpresenter's content?
Any help is much appreciated!
Thanks in advance
Where did you define the Data Template? It sounds like you are creating them as Window resources, and did not include it in your DialogWindow. If you're defining them as Window resources, the Data Template definition needs to be included on every Window you want to render this way. If the ViewModel/View pair is global to the application, it is easier to just define it in the App.xaml where it will be picked up by any Window or UserControl throughout the application.

How to define usercontrol in a page in xaml in windows 8, and reuse it in the same page?

i want to use a usercontrol, (set of TextBlock & Combobox). I want 3 instance of it in the same, page. So how can I define such usercontrol in the same page's xaml? need to use page resource? or anything else?
Within Visual Studio you can define UserControls by going to Project --> Add New Item and selecting User Control. After defining it there you can then add a reference to it in the XAML of the page you want to use it in. You do this by adding a something along the lines of the following to the root tag of your page.
<common:LayoutAwarePage
...
xmlns:CustomControlName="using:CustomControlNamespace"
...>
If you have to do it in the same XAML document, I guess you could define the control in the pages resources
<Page.Resources>
<UserControl x:Name="CustomControl">
...
</UserControl>
</Page.Resources>
Personally I would define a UserControl in a separate file. It separates things out and Visual Studio also gives you some basics to work from.

How to insert windows phone page to pivot item?

I have a problem with Pivot control. How can I insert xaml page with content to Pivot item? Is it even possible?
When I’m storing everything in one file it’s really hard to take control over my own application layout.
I want something like this:
on main page should be pivot control with items.
content of those items should be in separated xaml files (there is no dependency between pages)
Add correct namespace to Page:
xmlns:view="clr-namespace:YourApp.Controls"
and in Pivot:
<Pivot>
<PivotItem Name="first">
<view:FirstUserControl DataContext="{Binding YourViewModel}"/>
</PivotItem>
</Pivot>
And in view namespace you need to create a User Control. The link is about WPF but in WP7 there is the same system.
Also, you can create Custom Control from PivotItem.

WPF: How To Customize Generic Custom Window?

This is a tough question, but I'll try to explain anyway...
I have a custom control window that is used all over my applicaton. The reason I did this is because I wanted the various windows and dialog boxes to be fully customizable across my program. I.e., the minimize, maximize, close button and frame are all custom. This window is templated inside my generic.xaml. Now this works and it's all good. The idea I got was from http://www.codeproject.com/KB/WPF/CustomFrames.aspx
Now the users of this custom window are user controls in their xaml they basically use MyWindow as their root element:
<MyWindow>
....
</MyWindow>
But now what I'm trying to do is "inject" certain elements into MyWindow from the User Control's xaml. MyWindow would simply have a container for hosting them. For example, they might want to inject a toolbar button that appears right next to the minimize button. So for example, I might have a user control that does the following (where MyWindow is the root element):
<MyWindow>
<MyWindow.ToolBar>
<Button x:Name="BlaBla"/>
</MyWindow.ToolBar>
</MyWindow>
This would put "blabla" right next to the minimize button for example. But I'm wondering if it's even possible to do this. I.e., the whole MyWindow.ToolBar thing. Is there a construct for this, is this an attached property or something weirder?
It definitely is possible, depends on your choice of types for the DependencyProperty. You could use IEnumerable and bind the MyWindow.ToolBar dp to the ItemsSource on your internal ToolBar.
<ControlTemplate>
<!-- ... snipped down to the ToolBar ... -->
<ToolBarTray>
<ToolBar x:Name="PART_ToolBar" />
</ToolBarTray>
</ControlTemplate>
With the appropriate code in OnApplyTemplate to pull PART_ToolBar and create new Binding for the ItemsSource.
EDIT: rereading your question it appears that I missed that you wanted to add this elsewhere. My suggestion then would be to use this as an object dependency property, with a ContentPresenter bound to the MyWindow.ToolBar with a Visibility set if the binding is not {x:Null}.

New Window in a TabItem?

Is it possible to open another Window in a TabControl's TabItem?
The reason I want to do this is that if I have 5 TabItems in my TabControl, the one Window file I'm coding all these TabItems and their associated actions will get very large. So it would be nice if it was possible to to give each TabItem its own Window file.
Or how do you solve the problem where theWindow file controlling the TabControl gets too large?
<Window ...
xmlns:local="clr-namespace:MyNamespace"
>
<TabControl>
<TabItem Header="FirstTab">
<local:MyFirstTabUserControl/>
</TabItem>
<TabItem Header="SecondTab">
<local:MySecondTabUserControl/>
</TabItem>
<TabItem Header="ThirdTab">
<local:MyThirdTabUserControl/>
</TabItem>
</TabControl>
</Window>
Your each TabUserControl is actually simple UserControl, since TabItem can host any control as its own child.
You have several choices:
add one or more resource dictionaries to your app that contain resources with templates and styles for the various views you host in your tabs. This approach works well if you just need to maintain separation of the visual trees only.
create user controls for each view (with own XAML and class file) and use one instance for each different view in the tabs. This approach allows you to encapsulated specific business logic and the corresponding visual tree together.
generate some of the UI from code. This one has no advantages, except t makes you XAML smaller. And is your .cs files become too big, you can always split them in multiple code files and use partial classes. (just had to throw in this one for completeness :-))
You can also set the TabItem Content equals to your Window content
ex: [WindowType is your window]
WindowsType oWindow = new WindowType();
TabItem oTab = new TabItem();
oTab.Content = oWindow.Content;
Make a UserControl for each TabItem.
You can use a UserControl as was mentioned already.
But you can also use the Page control. Each "Window" would be a page, and the tab would have a NavigationControl in it. I've used this approach before and it works well. I prefer the UserControl route, but both are valid.

Categories

Resources