How can I separate `<style>` from `<Window.Resources>`? - c#

I have a lot of <Style> in <Window.Resources>.
How can I separate <Style> from <Window.Resources>?
Do I have to make a new Window file and use <Application.Resources>?
I also have a lot of those.
in my <Window.Resources>. Is it also possible to separate those?

You add those styles in ResourceDictionary and use it where ever required. You can add application level or Window Level.
http://blogs.msdn.com/b/wpfsldesigner/archive/2010/06/03/creating-and-consuming-resource-dictionaries-in-wpf-and-silverlight.aspx
http://www.codeproject.com/Articles/35346/Using-a-Resource-Dictionary-in-WPF

You can create a ResourceDictionary file. It's just a xaml where you can put styles, control templates etc. See here for more info: http://msdn.microsoft.com/en-gb/library/cc903952(v=vs.95).aspx
You can then attach this dictionary to your project in app.xaml.

Related

.NET 6 - WPF CustomControl Template not applied though being in App Resources

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)
)]

Stop CSS inheritance

I have a Blazor component, which I want not to be afected by the General Styles of the projects that use it.
The main problem is that the project that implements this Component, overrides some of the styles in their general styles.css, and it breaks the layout (principally because their project overrides some Bootstrap names).
I have been searching and found the CSS property all:revert; that I could add it to the first line of my component styles like this:
* {
all:revert;
}
So all the previous code that was modifying the component won't affect it.
But I'm not sure if this is the best solution to this problem.
Any help is appreciated.

GTK+ Glade and CSS

I've made a simple glade UI file for my programm. It contains of 2 textfields and 1 button. And also I made a css style which contains 1 button class named "button". When I added this file in Glade as "Custom CSS provider" it changed my button style within the editor.
After that I created the UI using GtkBuilder (I am using GTK# 3.20 bindings + C#).
Builder b = new Builder ();
b.AddFromFile ("login.xml");
b.Autoconnect (this);
It created my simple form (2 fields and 1 button) but there is no CSS Style applied to it. So I checked XML code of my UI file and found out that there is no reference to CSS exept of this:
<style>
<class name="button"/>
</style>
After that I read that I need CssProvider. So I created one.
CssProvider css = new CssProvider ();
css.LoadFromPath ("ui_common.css");
But nothing happened. So, how should I apply custom CSS to my Builder UI?
You are almost there; the last step is to take the GtkCssProvider and add it to a GtkStyleContext.
Since you are using a .ui file and a .css file, I assume you want to make your CSS apply to all widgets in the application. In that case, you can use gtk_style_context_add_provider_for_screen(), passing gdk_screen_get_default() as the GdkScreen. (This should be safe; it's what the GTK+ Inspector does. IIRC there was a more specific reason why this should be safe...)
You can also do this to individual widgets using gtk_widget_get_style_context() and gtk_style_context_add_provider().
In both cases, use GTK_STYLE_PROVIDER_PRIORITY_APPLICATION as the priority for application-local CSS.
(I do not know the C# names; sorry.)

Accessing resources from code for setting NotifyIcon.Icon

I am trying to get the Icon of a NotifyIcon in WPF.
So I have added a .ico file to my solution in a Resources folder and set the build action to Resource.
I am trying to grab this resource in code behind like so:
var icon = (Icon) Application.Current.FindResource("/Resources/icon.ico")
This doesn't work.
In addition to this: Application.Current.Resources.Count returns 0.
EDIT
var i = new Icon(Application.GetResourceStream(new Uri("/systemtrayicon.ico", UriKind.Relative)).Stream);
With the icon in the root and the build action set to Resource.
Still not working.
EDIT AGAIN:
I needed to Clean the solution and rebuild as per: WPF throws "Cannot locate resource" exception when loading the image
This will works 100%
ni.Icon = new Icon(Application.GetResourceStream(new Uri("pack://application:,,,<Image Location From root>")).Stream);
Example:
notify.Icon = new Icon(Application.GetResourceStream(new Uri("pack://application:,,,/images/favicon.ico")).Stream);
You have to pass resourceName as a parameter to the FindResource method, not the path for the Resource. Sample code would look like:
var icon = (Icon) Application.Current.FindResource("myImage")
Please note in the above sample code "myImage" is the resource name.
Refer to Application.FindResource Method on MSDN.
You say, Application.Current.Resources.Count is Zero, that means you do not have any Resource defined in your App.xaml file.
You can add resources to App.xaml like this:
<Application.Resources>
<Image x:Key="myImage" Source="img.png" />
</Application.Resources>
It appears that your icon is an embedded resource. FindResource cannot work with embedded resources. Set BuildAction of your icon to Resource.
Refer to this MSDN page for more reading on WPF Resources.
UPDATE
Code for accessing Embedded Resources
Assembly.GetExecutingAssembly().GetManifestResourceStream("myImg.png");
However, if you had added this image to the Resources.Resx and you should simply be able to use Resources.ResourceName.
UPDATE 2
Adding resources to App.xaml or any ResourceDictionary is better, so that you can use them as Static/Dynamic resources via StaticResource or DynamicResource markup extensions.
If you do not want to add it to App.xaml resources and still want to access it, one option as I mentioned above is to add it to the Resources.Resx and use Resources.ResourceName to refer the icon/image
Another way is to create System.Drawing.Icon by yourself, sample code:
new System.Drawing.Icon(Application.GetResourceStream(new Uri("/Resources/icon.ico")));
Personally, I would go with XAML resources and add them to App.xaml or a ResourceDictionary.

How to reuse usercontrols accrossdifferent WPF projects

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.

Categories