Applying style to DatePicker calendarStyle Property failes (WPF) - wpf

I am trying to create a custom date picker. I have created a WPF custom control Library. Renamed the control and changed the override meta data to the following.
public class SeasonDatePicker : DatePicker
{
static SeasonDatePicker()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(SeasonDatePicker), new FrameworkPropertyMetadata(typeof(SeasonDatePicker)));
}
}
After this i started to work out the template in the Generic.xaml file. First i was working with static resources to refer to the different styles (one style for Calandar and one for CalandarItem). researching the web i was able to find that it is not possible to define multiple resource in the Generic.xaml file.
So i changed to inline styles but still the change is not visible.
When i did the same thing in a WPF application it al worked. So i am at a loss right now.
The full Generic.xaml file is to big to post here but i have uploaded it: Generic.xaml
Since i am pretty new to WPF and styling any help would be appreciated.

Related

StyleTypedProperty Attribute not working in WinRT designer for custom control

I have created my own custom control in WinRT, which has own template for it. My custom control contains many inner custom elements(Small Controls) which are also having own templates.
Then i tried to provided the designer edit facility in Visual Studio for all that templates by StyleTypedProperty attribute and it results fail to me. In designer,EditTemplate working fine for my Control but EditAdditionalTemplates does not contain the templates of inner elements of my control.
NOTE: Same working fine in WPF designer.
Any one faced this kind of issue or please provide some suggestions?
[StyleTypedProperty(Property = "PopupStyle", StyleTargetType = typeof(Popup))]
public class MyControl : Control
{
}

Overriding some theme styles and properties per module

I am writing a WPF application using "Prism", and some purchased Grid theme is applied "Xceed Theme." I am designing a huge change required by our customer to give them the option of modifying some properties (like the background of selected row, for example) and this functionality to be available per screen "Module."
So my questions are:
After adding my xceed grid theme source to my App.xaml merged dictionaries, how can I override some of its styles (that I know the keys of) in another xaml file away from app.xaml (possibly by adding BasedOn to the style tag)?
Is it possible to create a custom resource dictionary for each module and add it to the app.xaml merged dictionaries while loading?
You could add the style changes in the main window/control's resources within each module. Then the differences would apply to each module separately.
In my case, I have userd Dynamic Resources. The main project loads the main style. somthing like this :
<FontFamily x:Key="ApplicationFontFamily">Tahoma</FontFamily>
and in my module i have changed it programmatically :
Application.Current.Resources["ApplicationFontFamily"] = new FontFamily("Arial");
it worked for me ;)

Localization in Silverlight 4 application using PRISM

I am new to silverlight development and PRISM framework. We are using http://happynet.codeplex.com/ as a template. The localization is working well if resource text is bind directly in XAML. But it is not working well for :
Texts in ViewModel Classes.
Grid Header Column Texts in View's Code behind.
They requires reloading after language switch. How can it be done so that text change occurs on language switch?
I don't know much about Happynet as it is the first time I see the project.
It depends how they implement localization but if you want dynamic localization (eg. selecting a language in a combobox) it can be done. Follow the following steps to set it up:
http://vanderbiest.org/blog/2009/11/30/silverlight-resource-files-localization-language-specification/
To dynamically change it, you just need to set the current UI thread.
static void SetLanguage(string culture)
{
if (culture != System.Threading.Thread.CurrentThread.CurrentUICulture.TextInfo.CultureName)
{
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);
}
}

WPF : Definition Control Template by programmatically

We can make WPF Custom Control for overriding some control's look&feel, like icon button.
public class MyButton : Button {
static MyButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyButton), new FrameworkPropertyMetadata(typeof(MyButton)));
}
...
}
But I think that way has a some problem. It causes a some issue when I distribute the Custom Cotrol 'MyButton'. Because MyButton dependent on external resource, the WPF Style MyButton. So I need to distribute two files : MyButton.cs and MyButton.WPF.
So, How can I definite a Conotrol Template by programmatically?
(Of cause, another way to solving the problem is making WPF User Control. but my point is not that.)
Note : I found some resources about this issue. That was a Inline XAML scripting. But to me, the XAML scripting is not option. Because I'm learning on WPF so I want to know WPF thatself, not a trick.
You do not distribute the code, you distribute a dll that contains the class and the generic.xaml
That way another designer/developer can 'override' the template and your template stays as a safe fall-back.
EDIT
Defining a Template in code is no fun (a lot of code, hard to debug, hard to maintain) but it can be done:
var template = new ControlTemplate(typeof(MyControl));
EDIT2
Another hack is to specify the template in a long string and use the XAML Parser to load it.

WPF: Applying a custom theme (skin) to a custom control

I have an application set up so that I can adjust its skin at run time. I am doing it as follows each time I change the skin:
Uri uri = new Uri("SomeThemeAssembly;component/Themes/StandardTheme.xaml", UriKind.Relative)
Application.Current.Resources.MergedDictionaries.Clear();
ResourceDictionary resource = (ResourceDictionary)Application.LoadComponent(uri);
Application.Current.Resources.MergedDictionaries.Add(resource);
This works fine.
I have also created a custom control with its controltemplate defined in Themes/Generic.xaml using ComponentResourceKeys for the relevant resources, etc
x:Key="{ComponentResourceKey
TypeInTargetAssembly={x:Type local:MyCustomControl},
ResourceId=MyBrush}"
The style of the custom control itself doesnt use a componentresourcekey as I want it to style all instances.
<Style TargetType="{x:Type local:MyCustomControl}">
Again this all works fine and standard controls I have used to compose elements of my custom control are appropriately styled when I change the skin.
However there are a number of custom properties( brushes, widths, etc) on my control that need to be styled to fit in with the skins I am applying.
In examples I have seen using the standard windows themes adding an additional file
Luna.StandardColor.Xaml
for example, to the themes directory of the custom control allows a modified control template to be set and picked up when that particular theme is applied. This won't work for me, as far as I can tell anyway, as my skins are not themes.
Can something similar be achieved when using 'skins' in the manner I have?
Obviously I can add Styles and ControlTemplates to my skin assemblies but this feels wrong especially as there is a standard naming convention across the skin assemblies for resources. So only a single more maintainable style would be required if it could be stored in Themes/StandardTheme.xaml in my custom control's assembly.
Having tried to read around on this subject I have the impression want I want to do is either impossible or will require loads of extra leg work.
Any thoughts appreciated.
In the CustomControls Themes Directory add a MyNewTheme.xaml file that's a resource dictionary that has an implicit style set for your control. Then merge that resource dictionary in with your other one as needed. For example:
Uri uri = new Uri("SomeThemeAssembly;component/Themes/MyNewTheme.xaml", UriKind.Relative)
Uri controlsUri = new Uri("ControlAssembly;component/Themes/MyNewTheme.xaml", UriKind.Relative)
Application.Current.Resources.MergedDictionaries.Clear();
ResourceDictionary resource = (ResourceDictionary)Application.LoadComponent(uri);
Application.Current.Resources.MergedDictionaries.Add(resource);
ResourceDictionary resource = (ResourceDictionary)Application.LoadComponent(controlsUri);
Application.Current.Resources.MergedDictionaries.Add(resource);

Resources