I have an WPF solution and this solution consist of 3 project:
1-A project that has several WPF user control inside
2-Another project that has several WPF user control inside
3-A project which has Resources for 2 WPF projects above.
As you know, if you have common settings for you views like that
-Using Same FontFamily.
-Using same FontSize
-Using same FontWeight
-Using same BackroundBrush for all your User Controls etc.. You need to declare this setters in you all usercontrol tags like below:
<UserControl ....
FontFamily="{DynamicResource MyFontFamily}"
FontSize="{DynamicResource MyFontSize}"
FontWeight="{DynamicResource MyFontWeight}"
Background="{DynamicResource MyAppBgBrush2}"
Width="250" d:DesignHeight="350">
<Grid/>......
But I dont want to write same setters in all my UserControls. For thi reason, I decided to move this property setting in to a new c# file and locate it in Resource Project.
using System.Windows.Controls;
namespace Resources
{
public class PageBase : UserControl
{
public PageBase()
{
SetResourceReference(FontFamilyProperty, "MyFontFamily");
SetResourceReference(FontSizeProperty, "MyFontSize");
SetResourceReference(FontWeightProperty, "MyFontWeight");
SetResourceReference(BackgroundProperty, "MyAppBgBrush2");
}
}
}
So, In my Resource project, I adited AssemlyInfo.cs file like this:
[assembly: System.Windows.Markup.XmlnsDefinition("http://schemas.sat.com/winfx/2010/xaml/internalresources", "Resources")]
This edit gives me ability to declare/create a user control like below:
<internalresources:PageBase
xmlns:internalresources="http://schemas.sat.com/winfx/2010/xaml/internalresources">
<Grid>DoWhatEver<Grid/>
<internalresources:PageBase/>
From now, I do not have to create a usercontrol view which its tags start with
<UserControl...., I can start with <internalresources:PageBase......
My Question is that, VisualStudio 2010 can show me Design of all my user control bu Expression blend can not. Interesting part is that both in VS and Blend, my project compiling without any error But when I try to open my views in blend it says:
-The namespace 'PageBase' does not exist in namespace "http://schemas.sat.com/winfx/2010/xaml/internalresources"
P.S: References are added properly to my Project and My project was suitable to open with blend.
Related
We are switching a huge WPF Appl. to .NET 6.0. At least one CustomControl which worked on 4.8 does not get it's template applied which is referenced via Generic.xaml. I am not sure if Generic.xaml is not loaded or something else needs to be considered.
The Style can be added manually to Application.Resources.MergedDictionaries via a simple "Add" call with the Source set to it. I can see the CustomControl Style afterwards with the Key being the correct Type. It is still not applied, as there is no visual representation and no call is made to the overriden OnApplyTemplate method.
If all else fails, can I apply a template manually if I have the given style, like just apply the Style manually to a newly created instance?
Also: the Projects are now SDK-Style, AssemblyInfo.cs was taken over and "generate assembly info" is set to false. It contains the standard ThemeInfo entry.
Just for clarification following the code which successfully finds the Generic.xaml. But before 6.0 Generic.xaml was loaded without doing anything.
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("/Contracts;component/Themes/Generic.xaml", UriKind.Relative)
});
Thank you all for your help!
I had the same Problem.
maybe u need to add the Assembly.cs at TopLevel of ur CustomControlLibrary
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
If I extend an existing object, like a DataGrid:
public class CustomDataGrid : DataGrid
{
static CustomDataGrid()
{
CommandManager.RegisterClassCommandBinding(
typeof(CustomDataGrid),
new CommandBinding(ApplicationCommands.Paste,
new ExecutedRoutedEventHandler(OnExecutedPaste),
new CanExecuteRoutedEventHandler(OnCanExecutePaste)));
}
...
On the xaml side of things, if I try using a <CustomDataGrid/> I get something like, CustomDataGrid is not supported in a Windows Presentation Foundation (WPF) project. So how do I actually use the extended class on the xaml side?
You need to reference the class by namespace. This involves adding a namespace declaration to the top of your Xaml file, and then using that namespace in your control element.
If we assume that your CustomDataGrid is in a namespace called Rhubarb, in the same assembly as the Xaml you're writing , you'd need to add this attribute to the root tag in your Xaml file (alongside the other xmlns attributes):
xmlns:rhubarb="clr-namespace:Rhubarb"
Then, where you declare your grid, use this element instead:
<rhubarb:CustomDataGrid />
If your cod is in a separate (referenced) assembly, you need to modify the namespace declaration thus:
xmlns:rhubarb="clr-namespace:Rhubarb;assembly=NameOfYourAssembly"
(Note that there's no .dll suffix on the assembly name.)
I am developing a Windows Store project, and I wish to move MainPage.xaml into a folder called Views. After that I want to rename it. I already attempted doing this, but I ended up breaking it (InitializeComponent could not find a definition).
How can I move and rename the page properly?
Actually, it seems that the MainPage type is hard-coded in the auto-generated code.
look at this post to get information on how to change it.
Let's say you renamed it to MyView.xaml, and moved it to Views folder.
You will probably want (not necessary) to also:
1. add ".Views" to the namespace in MyView.xaml.cs
2. add ".Views." to the x:Class tag in MyView.xaml
Now open App.cs, and locate the following line
if (!rootFrame.Navigate(typeof(MainPage), args.Arguments))
{
....
}
change MainPage to reference your new page name.
Simply make sure to keep the namespaces between the code behind and the xaml synchronized:
MainPage.xaml.cs:
namespace YourNameSpace.Views
{
public partial class MainPage : UserControl
{
}
}
MainPage.xaml
<UserControl x:Class="YourNameSpace.Views.MainPage"
....>
</UserControl>
Quick note: It's a little different on Windows Phone: the startup page is defined in the DefaultTask section of the WMAppManifest.xml file:
<Tasks>
<DefaultTask Name ="_default" NavigationPage="MainPage.xaml"/>
</Tasks>
This all began as an attempt to have a numeric textbox. That part is irrelevant but it's why I created the following class. (By the way, using VS 2012 Express, WPF, C# code-behind).
using System;
using System.Windows.Controls;
namespace Herculese
{
public class IntBox : TextBox
{
<!-- irrelevant code here-->
}
}
So far, so good. I build and this becomes a control which I proceed to use in the xaml:
<local:IntBox Name="txtBox_heightft" Width="60" TextChanged="txtBox_Numeric_Changed" />
Then in my code behind where I'm trying to refer to the text in the textbox using "txtBox_heightft.Text", I'm informed that "The name 'txtBox_heightft' does not exist in the current context". This confuses me to no end because if I change "local:IntBox" to "TextBox" in the xaml, it works fine but then of course it's a regular textbox and not my modified version. Do I need to add a reference to the class in the codebehind somehow? This is my first attempt at using a class this way, as I've never needed functionality that wasn't provided by default.
The problem is that you are using Name as a dependency property, you need to use x:Name="txtBox_heightft" as an extension property :)
I have a WPF project say Application 1 and inside of it a user control is defined ..I want to resue this user control in another WPF project ,How I will be able to do it??
Both projects are binded into same source control
I am not an expert but let me try. Ensure that your user control is accessible from another project, i.e. there is a reference to the library where this control is defined. Then, at the place where you want this control to be reused, you need to define the namespace and use your control as any other controls. e.g.
<Window x:Class="Test.Window1" Title="Window1" Height="300" Width="600"
...
xmlns:userControls="clr-namespace:MySolution.MyPresentation.MyControls;assembly=MySolution.MyPresentation"
/*controls namespace and assembly*/
>
<Grid>
<userControls:ReusableControl
/*particular properties of ReusableControl */
/>
</Grid>
<window>
best way is:
define your user control in other project only contains your user control as Class library project (Like Scott Say)
but stil you can do this manually with these steps:
copy your user control from App1-pro(from solution explorer)
to App2-pro(even you can don this
from vs to another vs)
open Code
behind file and go to Ctor and find
InitializeComponent-mthod push
f12(or use goto definition from
right-click menu)
change the name
space from application1 to
Application2 namespace(oops save
it!)
change cod-behind-file
namespace like above step(also save
it)
go to xaml file and change
this
x:Class=WpfApplication1.UserControl to x:Class=WpfApplication2.UserControl
Now enjoy your User-control!
Refactor your control into a seperate windows library (dll), and then add that dll to the references for each project.