I'm trying to develop Custom Window, which i can reuse in other applications.
I know that WPF cannot derive from XAML
I also tried to deploy it as Class Library, the code provided in this
video:
http://www.youtube.com/watch?v=EuhhL_NF-B0&feature=c4-overview&list=UUjwAVugYBMQemsMi9AD4SZA
, but still it does not read the XAML file.
I tried with code-behind to set the ControlTemplate, but as i read FrameworkElementFactory is deprecated...
All i want to do is, derive from Window, change the ControlTemplate, release it as Class Library... anyone can show me how or point me to the right direction?
In my opinion the best solution will be to create custom UserControl, and then load it from Window.xaml.
Once you created your user control just load it from Window like this:
<Window xmlns:my="clr-namespace:Styx.GUI.View" x:Class="Styx.GUI.View.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="800" Width="650" MinHeight="600" MinWidth="600">
<my:MainWindowUserControl />
</Window>
Related
In my WPF application, Need to show a Non-modal window.
I am using MVVM Light framework.
People are suggesting different libraries to do so but is there any suitable control to do that using MVVM Light or using WPF native library?
Need to keep that non-modal window always on top.
Thanks.
To create a non-modal window you won't have to use a framework. The WPF library has enough possibilities to create it.
The quickest solution is to create a new Xaml Window and within the properties of the Xaml Window code, you van specify the TopMost priority, and set it to true.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="NonModalWindow" Height="300" Width="300" TopMost="True">
<Grid>
<!---- Some element defined in your window ---->
</Grid>
</Window>
Once you have created this window it is only a matter of calling it.
new NonModalWindow().Show();
And if you do want to make it into a Modal window you can use to following code.
new NonModalWindow().ShowDialog(); // wooah a contradiction in the code
I have custom ContentControl
public class MyContentControl: ContentControl
{....}
with Content defined in XAML like this
<controls:MyContentControl x:Name="myContentControl">
<controls:MyContentControl.Content>
<controls:UserControl1 />
</controls:MyContentControl.Content>
</controls:MyContentControl>
Content shows in designer and in the device when I launch my application. But when I try to change Content property programmatically, for example
UserControl2 control2 = new UserControl2();
myContentControl.Content = control2;
MyContentControl shows nothing. Using standard ContentControl give the same result.
Any suggestions are welcome.
I followed your code to make simple code sample to test. There's no problem.
public class CustomContentControl:ContentControl
{//......}
<Grid>
<local:CustomContentControl x:Name="content">
</local:CustomContentControl>
</Grid>
MyUserControl1 myUserControl1 = new MyUserControl1();
content.Content = myUserControl1;
<UserControl
x:Class="AppContent.MyUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AppContent"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="400">
<Grid>
<TextBox Text="abc"></TextBox>
</Grid>
You might have done some specific settings in your code. #Martin Zikmund's suggestion also was reasonable. You could refer to his suggestion and check your code. After that, if you still could not solve this issue, please provide a Minimal, Complete, and Verifiable example.
This should work. The reason could be that the control does not stretch and is displayed just 0x0 in size. Try to set absolute Width and Height to the control2 and check if it displays. You can also set myContentControl.HorizontalContentStretch and myContentControl.VerticalContentStretch.
You can try running the app in debugger and then use the Live Property Explorer to see what the actual size of the control inside Content is.
Ok, I found out where the things went wrong. I am using different controls for desktop and mobile devices, so I put some of theirs XAML views to the DeviceFamily-Mobile folder. This way they automatically use when needed. I've confused namespaces, because all XAML views in this folder have a root namespace for accessibility reasons. When I was trying to add control to the ContentControl via c#, I didn't resolve namespace where my controls were placed. So I've put XAML view as a childs to the ContentControl, and they staying invisible as none of them has InitializeComponent() method. Adding correct controls with initialization fixed my problem.
I am very grateful for your answers, they pointed me to the right way.
I made a WPF control in a library project and would like to extend it with a new one.
<UserControl x:Class="Genesyslab.Desktop.Modules.ExtensionUtils85.GUI.EmbeddingUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
</Grid>
</UserControl>
I have tried to extend it like this:
<src:EmbeddingUserControl x:Class="Interaxalab.Desktop.Modules.PrototipoCable.CustomViews.InteractionView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="Genesyslab.Desktop.Modules.ExtensionUtils85.GUI"
Name="InteractionWorksheetView" Height="321.613" Width="471.396"
>
<Grid>
<WindowsFormsHost x:Name="windowsFormsHost1" HorizontalAlignment="Left" Height="284" VerticalAlignment="Top" Width="471"/>
</Grid>
</src:EmbeddingUserControl>
However, I get an error message saying that the name "EmbeddingUserControl" does not exist in namespace "Genesyslab.Desktop.Modules.ExtensionUtils85.GUI".
The name clearly does exist, since the xaml.cs can find it, but for some reason the xaml cannot.
Any help would be appreciated.
Long story short - you cannot inherit control with xaml by another control with xaml (and does it makes sense even to do so?). In your case, EmbeddingUserControl does not contain any visual tree elements (just empty grid), so you can just do:
public class EmbeddingUserControl : UserControl {
// some methods, properties of your control
}
Then you can inherit exactly like you do already in your question (don't forget to inherit from EmbeddingUserControl both in xaml file and in code-behind file).
You can also inherit from user control with xaml, if your inherited control does not have xaml itself (so you can add\override logic).
If you need to inherit some visual stuctures - you have to switch from inheritance to composition. That is: your base control provides some placeholders where other controls may be placed, or even better allows to provide templates to format data items (like for example ItemsControl and ItemTemplate property). Then you just fill those placeholders with other controls if necessary.
Both of these applications are rather old and have been built and maintained over several years by several people. At the moment, one of controls used in the WinForms project really needs to be displayed in the WPF project.
I've read about using WinForms controls in WPF projects, and for the most part if you're just instantiating a regular empty WinForm control, it seems relatively simple.
What I'm wondering is how you would best approach using part of a large project in another project? Ideally the WinForm control will be visible from within ONE of our WPF controls, on ONE tab, after having been sent and loaded the required data.
Here are some general guidelines.
From your WPF application, add project references to:
your WinForms project
WindowsFormsIntegration
System.Windows.Forms
Modify your XAML to include a WindowsFormsHost:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TabControl>
<TabItem Header="Old Form">
<WindowsFormsHost Name="WinFormsHost"></WindowsFormsHost>
</TabItem>
</TabControl>
</Grid>
</Window>
Instantiate your old Form and set it as the child of the WindowsFormsHost. Set TopLevel to false or it'll complain that "the child control cannot be a top-level form." Change the FormBorderStyle too, to prevent the Form's title bar from showing up and allowing the user to drag the Form around.
public MainWindow()
{
InitializeComponent();
WinFormsHost.Child =
new Form1 { TopLevel = false, FormBorderStyle = FormBorderStyle.None };
}
You end up with something like this:
You can read more in "Walkthrough: Hosting a Windows Forms Control in WPF" and the MSDN documentation for the "WindowsFormsHost Class".
I'm new to WPF programming and decided to give it a shot by trying out some ribbon control libraries.
The library that looks best for now is the Microsoft RibbonControlsLibrary. You can get it on the ribbon licensing page.
So far I've started a new project, added the control to the windows, but them I'm stuck: This is the code so far:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="808" xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" xmlns:my1="clr-namespace:Microsoft.Windows.Controls.Ribbon;assembly=RibbonControlsLibrary" WindowStyle="SingleBorderWindow">
<Grid>
<my1:Ribbon HorizontalAlignment="Left" Name="ribbon1" VerticalAlignment="Top" Height="165" Width="786" ShowQuickAccessToolbarOnTop="False" WindowIconVisibility="Visible" DataContext="{Binding}" Margin="0,-20,0,0">
<my1:Ribbon.ApplicationMenu>
<my1:RibbonApplicationMenu Visibility="Hidden" IsEnabled="True" />
</my1:Ribbon.ApplicationMenu>
<my1:RibbonTab Label="Tab1" Name="rtab1" >
</my1:RibbonTab>
<my1:RibbonTab Label="tab2" Name="rtab2"/>
</my1:Ribbon>
</Grid>
</Window>
Questions:
1) Where can I find samples for this ribbon control? I've tried googling, but came up with nothing useful.
2) How to add items to specific ribbon tabs? I'm lost in all these properties in the property grid. So far I havent found a designer for that purpose.
3) How can I switch the designer to show me what icons/button/... I placed on TabPage2?
(FYI: The fluent ribbon library does not seem to work for me, because I can't get rid of the ApplicationMenu.)
Found a great sample/tutorial:
http://windowsclient.net/downloads/folders/hands-on-labs/entry76491.aspx
The sample provides a manual with explanations and some test projects with step by step instructions to implement the ribbon control.
Though I'm totally new to WPF, I managed to extract necessary classes from the sample to provide a ribon based menu in my program.