WPF : Definition Control Template by programmatically - wpf

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.

Related

Applying style to DatePicker calendarStyle Property failes (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.

User control, custom control, inheritance and override

I know this question is probably going to get alot of "Duplicate question" comments but i have yet to see one with an answer that works or isn't simply "Avoid it at all cost". But here goes, i have created a control lets call it "ControlA" in a project called "ControlA_Project" and i was intending to override some of its virtual methods in a control called "ControlB" that inherites "ControlA" in another project called "ControlB_Project" . The idea being that "ControlA" has save and load methods that i wish to change from saving and loading to file, too saving and loading to database ( and if later on another save and load type is required i can just override those methods again ).
The problem i have is i originally had "ControlA" as a usercontrol and when i tried adding the new control with the overrides ("ControlB") into a window i would get this error:
The component 'ControlB_Project.ControlB' does not have a resource identified by the URI '/ControlA_Project;component/usercontrols/ControlA.xaml'.
Googling the error i came to the conclusion you could not inherit from a user control ( or for the sake of arguement it wasn't a good idea ). So i then changed the control from a user control to a custom control. this however then leads me to another problem, a template for a control doesnt link to the code behind (i.e On_Click) like it does in a user control and there is no easy way to simply override the behavier (as far as i am aware). I know i can retemplate ControlB by copy and pasting ControlAs template and changing a few lines but controlA has a large template as it is and making mutliple copies seems a waste of space.
So put simply is there a correct way to change the behavier of a control in wpf?
First, remember that the ControlTemplate can be changed by the user alot so you need to make sure that important fields are clearly marked.
So if you want an OnClick event. First mark your button as "important"
<Button x:Name="PART_MyButton"/>
Its also a good idea to mark this aswell on your control class
[TemplatePart(Name = "PART_MyButton", Type = typeof(Button))]
public class MyCustomControl : Control
Now you want to attach to the on click event of that button to do that, override the OnApplyTemplate method.
public override void OnApplyTemplate()
{
mButton = Template.FindName("PART_MyButton", this) as Button;
mButton.Click += MyEventHandler;
}
Depending on how well your control can work without the control, you should gracefully handle a not found control or throw an exception.
One final thing is. If you override a control which has a default style, it might be a good idea to provide a new default style.
static MyCustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), new FrameworkPropertyMetadata(typeof(MyCustomControl)));
}
and placing an implicit style in your generic.xaml

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 - Which one is better? Style or User Control?

I wanted to know which one amongst Style and UserControl would be better to use in WPF?
For example:
I have created an image button in two different ways.
One uses Style and ContentTemplate property is set.
It uses one other class with dependency properties.
The other way is I have created a UserControl which has a button and its content property is set.
The file UserControl.xaml.cs also contains the dependency properties.
For Code details see the answers of this question:
Custom button template in WPF
Which one would be better to use? In which scenario should one go for Style or UserControl or any CustomControl?
Styles are limited to setting default properties on XAML elements. For example, when I set the BorderBrush , I can specify the brush but not the width of the border. For complete freedom of a control’s appearance, use templates. To do this, create a style and specify the Template property.
Styles and templates still only allow you to change the appearance of a control. To add behavior and other features, you’ll need to create a custom control.
For example,
To create a button like a play button use styles and templates, but to create a a play button which will change its appearance after pausing it use UserControl.
For this type of thing I would go with Style, even though I'm not really adept with graphical tools. I tend to produce a basic, boring style that I can get started with and then prettify it once the application functionality has been verified.
The nicest thing about WPF is being able to distance much of the graphical look, feel and behaviour away from the code.
This allows you to change the style of your application without revisiting the code and indeed means that you can change styles on the fly at runtime.
There is an awkward line to tread with regards to how much behaviour is placed within the XAML and how much is placed within the code. A rough guide would be to decide on what behaviour must always be present within the UI and place that in the code, everything else place within the XAML.
Think of the code as being an abstract class with defined interfaces and the XAML Styles as being classes based on that class and you'll get an idea of what I mean.
Conversely, I know that people who are far more adept at the GUI work prefer to put more functionality in the XAML and others who prefer the code side, because they find the GUI work slow or difficult.
When thought of that way you'll see that there's never really a right or wrong answer, just better solutions that suit your skills.

How to use inheriting in WPF

I try to use inheriting in WPF. I have asked a question about this earlier, but no one answered correct. So I try to make a BaseWindow class with some UI elements and I want that other windows, that inherit my BaseWindow would have these UI elements. How to do that. My practise with WinForms applications dont work anymore. Maybe there are some simple examples or smth..? Thanks
If you want to inherit from a window, you need to create an implementation of a window in code only, and you can then inherit from this - note that your Window declaration in the XAML would need to change to point to this code, e.g.
<src:BaseWindow xmlns:src="clr-namespace:BaseWindowNamespace" ...>
If you define the base window using XAML, you'll get the following errors:
"'WpfApplication1.Window1.InitializeComponent()'
hides inherited member
BaseWindowNamespace.BaseWindow.InitializeComponent()'.
Use the new keyword if hiding was
intended"
"BaseWindowNamespace.BaseWindow cannot
be the root of a XAML file because it
was defined using XAML"
Now, at this point, I must point out that this is counter to the way that you should handle composition of your window. The "standard" way of doing this would be to use content templates to display user controls which could be swapped and styled to achieve different looks and functionality. In this way, the function of the window becomes that of a harness, and you can achieve a clean separation of content which can be easily implemented using MVVM.
Did you try the BasedOn tag? I don't know about windows, but that's what you use for UserControls AFAIK.

Resources