Iron Python Tools Visual Studio 2010 does not provide a way to add WPF User Control in a IronPython WPF project? How do I make one? I tried copy-pasting User Control from C# WPF project but it did not work. Please help.
It might not be quite the same as what you're asking, but I wanted to make a UserControl the content of a TabItem, to keep the XAML for the tabs in separate files.
I haven't found a clean way in IronPython, but I have found a reasonable hack. I imagine it would work for components other than TabItems, too.
First, I named my TabItem, like this:
<TabItem Name="my_tab_item" Header="My Tab Item" />
Then I loaded my UserControl's XAML into a custom class:
import wpf
import clr
clr.AddReference("System.Xml")
from System.Windows.Controls import UserControl
class MyUserControl(UserControl):
def __init__(self):
wpf.LoadComponent(self, 'my_user_control.xaml')
And I added it to the TabItem like this:
self.FindName("my_tab_item").Content = MyUserControl()
Related
I'm struggling with Templates in WPF
I understand the concept in that I can have a control, and 'override' the ContentTemplate (or similar). I use it often with the TabControl
However, I don't understand it in terms of what the initial control looked like. And if we are limited on the names. EG, could we build a control and overwrite the NonsenseNameTemplate?
If I were to build my own UserControl and provide the ability for people to override things like my NonsenseNameTemplate, what would this code look like?
My guess, with pseudo code would be
<UserControl>
<NonsenseNameTempalte>
//some resources
//some other controls
</NonsenseNameTempalte>
</UserControl>
In the above example, I can fully understand how I could create a new control and overwrite the NonsenseNameTemplate but I can't see any code examples of the UserControl and it's usage.
Template is nothing more than a (dependency) property. By writing
<Button>
<Button.Template>
<ControlTemplate>
...
</ControlTemplate>
</Button.Template>
</Button>y
you are doing nothing more than creating a new instance of ControlTemplate class and assigning it to Button.Template property of that specific button. Each control has it's own ControlTemplate saved somewhere in it's assembly. It is generally not so simple to actually get those default templates, but Blend can help with it.
Of course, you can create your won NonsenseNameTemplate property, but actually using it would require some deep knowledge of WPF composition, layouting and rendering. Which is usually not required for normal usage of WPF. And I agree that there is not a much online resources about doing something like this, for exactly this reason.
I too had an early on learning of Templates. I posted a step-by-step answer to another question via a customized button control. The nice thing about that sample, you can build and play with styles and templates in a small project and see visual impact without requiring full project rebuild
To start with, as you have mentioned that you do not have Blend. You can have Blend and install it with Visual Studio 2013 Community.
You can download this here if you do not have it already.
http://www.visualstudio.com/products/visual-studio-community-vs
A part of the installation process, it allows you to select and install Blend. Also, the newer editions of visual studio give you some of the power of blend. In your design view you can right click on a control and create or copy a template.
On to the question.
As Euphoric has mentioned. Custom control authoring does require a little more in-depth knowledge of WPF, or any of the XAML frameworks. However, there are Visual Studio templates that can help you in the right direction.
As for the template naming, you have three types of templates you will come across in XAML. ControlTemplate (which for your purposes is the one we are interested in), ItemsPanelTemplate and DataTemplate. Again, as Euphoric has said, there are few circumstances where deriving a custom version of any of these three templates would bring anything to the table.
If you were to create a test WPF application, once you have created the basic project and solution. Go a head and add in another project, and from the templates VS provides, File -> Project -> New -> Windows Desktop. In the project template list, find 'WPF Custom Control Library'. Once created, reference this project in your main WPF project.
This custom control library project will give you a skeleton setup for what you are looking for.
If you look in this project here are some things for you to note.
Firsty, you will find a folder called Themes and in there a file named Generic.xaml. In there you will see a style that has a setter setting the Template property. You will also see that both the Style and set ControlTemplate have a set TargetType that should be local:CustomControl1.
This is important as this is showing you how to create a custom controls default template. Now, to apply this template look in CustomControl1.cs and consider the following code.
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
For this control named CustomControl1, that template we looked at in the xaml will be automatically set as that controls default template where ever that control is used.
This project is a good starting point. But now you may want to override this ControlTemplate inside your main project. This is simple. I have code that looks something like this inside my main WPF project.
At the top of the MainWindow.xaml
<Window x:Class="CustomControlTesting.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:CustomControl="clr-namespace:CustomControlTesting.CustomControlLibrary;assembly=CustomControlTesting.CustomControlLibrary"
Title="MainWindow" Height="350" Width="525">
And in the body,
<CustomControl:CustomControl1>
<CustomControl:CustomControl1.Template>
<ControlTemplate TargetType="{x:Type CustomControl:CustomControl1}">
<Grid>
<!-- Define my look to override the template -->
</Grid>
</ControlTemplate>
</CustomControl:CustomControl1.Template>
</CustomControl:CustomControl1>
This is a brief overview and certainly misses out a lot. But I hope this is of help to you and can get you started.
I'm really new to WPF and I'm trying to change the hover colour of a button in WPF. I've figured out I need to create a control template in order to efficiently do this, which I've been able to successfully do in a standard WPF application which has a App.XAML file, however the application that I'm using isn't a full WPF app, it's a winforms app that uses a ElementHost to link a WPF user control into the form. Soooooooo I was wondering how do I create a control template for WPF user control? I don't have a app.XAML which is where I put the control template the first time I did it, and if I try to slide the control template into any of the user control XAML it throws an error.
Thanks
When you're creating the template in App.xaml you're adding it as a resource by putting it inside the Application.Resources resource dictionary. You can do the exact same thing on any other element in XAML that represents a FrameworkElement (i.e. any control, layout panels, etc.). The basic setup is like this:
<UserControl ...>
<UserControl.Resources>
<ControlTemplate x:Key="MyCustomTemplate" .../>
</UserControl.Resources>
</UserControl>
I have a WinForms User Control (a toolbar) which i would like to add on a WPF user Control, is there any way to do this?
like i want a WinForms User Control (the toolbar) along with other WPF Controls
(datagrid) on a new WPF User Control
I saw a couple of samples that show Windows Forms hosted in a WPF Control. But that is not what i want to see.
This is pretty easy to pull off. There is a handy little thing known as WindowsFormHost all you have to do is declare it in your control's XAML, and nest your forms control inside of it, like so:
<UserControl>
...
<Grid>
<WindowsFormsHost>
<forms:MyFormsToolbarControl/>
</WindowsFormHost>
</Grid>
...
WindowsFormHost lives inside of the normal WPF toolbox so it shouldn't be hard to locate. Meanwhile there is an example of how to produce the equivalent XAML in code at this location...
http://msdn.microsoft.com/en-us/library/ms751761.aspx
You can check out my answer to a similar question here:
WPF hosting a WinForm, Tab Navigation problems
This will also show you how to fix a tabbing issue with windows controls that are sitting inside wpf views.
I have created the WPF user control and WPF window in class library project. Controls are public.
These controls are available for other project of the same solution. I can see them in the toolbox of the VS2010 when i add project reference from the WPF application project.
However, when i create some WPF application project outside the solution and add reference to the my user control library project, the controls do not appear in the toolbox! (My applications XAML file is open when I try to see them in toolbox to drag-drop on the application's main XAML)
What makes the controls avilable to the outside world when their assembly is referenced by the consumers?
Try right clicking on the toolbox and selecting Choose Items From that form click browse and select your DLL. That should put the controls in the toolbox for you.
I think the problem is that in your new WPF project you need to set an XML namespace where you reference your user control library. Then you can use your controls in the XAML.
For example :
thats an xml namespace definition in the XAML:
xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;
assembly=Microsoft.Phone.Controls.Toolkit"
And how you can use a control from this control library
< toolkit:ListPicker />
I hope this will help you.
Right click inside the toolbox and select "Show All".
I am new in WPF. Created a new WPF UserControl. See that some people uses an app.xaml file in order to set inside application level ressources.
My solution consists of a WinForm and a WPF UserControl. I don't see somewhere any app.xaml file.
How to proceed?
App.xaml is associated with a WPF application. If you've only got a UserControl, there's no application for it to be part of, is there?
Create a WPF application and you'll have an App.xaml to put application-level resources in.
Out of interest, why do you have WinForm if you're using a WPF user control?
EDIT: To repeat my comment: you're not going be provided with WPF Application resources smoothly when you're not creating a WPF application.
EDIT: As noted in Anthony Brien's answer, it seems you can hack it around - but I would strongly recommend against this sort of thing if you can possibly help it. Fundamentally, you're working against the expectations of the platform - and that's never a nice situation to be in.
It is possible to have application wide WPF resources in a WinForms application. Look at http://www.wpftutorial.net/AppLevelResourcesWinForms.html
You need to create a WPF or Silverlight application to get that file
You are actually hosting a WPF control in a Winforms application
If you created a WinForm project, it will not have a app.xaml.
Create a WPF project instead.
This article explains in great detail how to have application wide resources in a hosted/interop scenario:
http://drwpf.com/blog/2007/10/05/managing-application-resources-when-wpf-is-hosted/
The solution path I chose (from the article) is to include App.xaml as a page element
Modify project file:
<Page Include="App.xaml" />
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
Add code:
public App()
{
InitializeComponent();
}
public static void EnsureApplicationResources()
{
if (Application.Current == null)
{
// create the Application object
new App();
}
}
Full details are in the article.