Not found SetResourceReference in new Groupbox I'm creating in code behind - wpf

I want to set a reference to a Template in a groupbox I'm creating in the code behind.In a example I found, they mostly use SetResourceReference method. However, I see that this method doesn't seem to exist on the new Groupbox property in creating.
XAML:
<ControlTemplate TargetType="GroupBox" x:Key="GBTemplate">
Code Behind:
groupbox gb_plat
gb_plat= create GroupBox
gb_plat.SetResourceReference(Groupbox.TemplateProperty,"GBTemplate")
This method have come out with 2 errors,
1.Undefined variable: TemplateProperty
2. Can't find element SetResourceReference

If you are setting the control template for your groupbox then you will have to do:
gb_plate.Template = Resources["GBTemplate"] as ControlTemplate;

Related

Setting FontFamily of TextBox applicationwide

I have a big project at hand which involves a large amount of views and usercontrols. Additionaly, I want to set the FontFamily of every element to a certain font.
This works with most of the usercontrols like textBlocks, buttons and labels. Sadly this does not hold for textBoxes. They remain unchanged.
Before I create the whole GUI, I am overriding most of the metadata for elements containing text:
TextElement.FontFamilyProperty.OverrideMetadata(typeof(TextElement),
new FrameworkPropertyMetadata(new FontFamily("Calibri")));
TextBlock.FontFamilyProperty.OverrideMetadata(typeof(TextBlock),
new FrameworkPropertyMetadata(new FontFamily("Calibri")));
After a bit of searching, I found this article using the same method: http://blog.davidpadbury.com/
It clearly states at the end:
"In the above image you’ll see that we’ve successfully change the font on the text blocks, labels and buttons. Unfortunately the font inside the TextBox remains unchanged, this is due to it receiving it’s FontFamily property from it’s base class Control. Control adds itself as an Owner of the TextElement FontFamilyProperty but specifies it’s own metadata which we are then unable to override."
It also suggests to create a control template, which then sets the fontFamily. Is there another way? I want to set the fontFamily programmatically at the start without using XAML or creating a controlTemplate and using it as a base template for every textBox.
Thanks in advance.
You can declare a Style without the x:Key property and it will apply to all controls of that type:
<Style TargetType="{x:Type Control}">
<Setter Property="FontFamily" Value="Calibri" />
</Style>
Alternatively, you can simply set this on the MainWindow definition which will affect most elements:
<Window TextElement.FontFamily="Calibri" ...>
...
</Window>
Ahhh... I've just noticed your condition of not using xaml... sorry, I should have looked closer the first time.
UPDATE >>>
After a little research, it seems that you can do this in code like this:
Style style = new Style(typeof(TextBlock));
Setter setter = new Setter();
setter.Property = TextElement.FontFamilyProperty;
setter.Value = new FontFamily("Calibri");
style.Setters.Add(setter);
Resources.Add(typeof(TextBlock), style);
Unfortunately, you'd have to do other Styles for other types of controls too.
UPDATE 2 >>>
I just thought of something... that previous example just set the Style into the local Resources section which would be out of scope for your other modules. You could try setting the Style to the Application.Resources section which has global scope. Try replacing the last line of the code example above to this:
App.Current.Resources.Add(typeof(TextBlock), style);

how to use style defined in xaml resource dictionary?

I have a a IMultiValueConverter that dynamically creates TextBlock controls. The issue is that it has no styles.
How can I tell my new TextBlock to use a style that was defined in my XAML resource dictionary?
See the following question: how to use DynamicResource in the code behind?
Use SetResourceReference, it's equivalent to use DynamicResource in Xaml
So if your Style has the Key myTextBlockStyle
TextBlock textBlock = new TextBlock();
textBlock.SetResourceReference(FrameworkElement.StyleProperty, "myTextBlockStyle");
I have never tried this before, and depending on what your converter is doing, I think if your XAML resource dictionary is external, then link it into the Window where you are displaying the TextBlocks:
<Window.Resources>
<ResourceDictionary Source="[the path to the resource dictionary]"/>
</Window.Resources>
Then in your textblocks, ensure they have the Style attached that is defined in the resource dictionary. If the textblocks are being created in code behind I believe you should be able to use FindResource to locate the style that is linked in by the resource dictionary. Then do something like this:
textBlock1.Style = (Style)FindResource("myTextBlockStyle");

WPF: Create a custom control without rewriting the ControlTemplate

Hey, I am creating a Custom Control i WPF inheriting from the ListView. However, I want it to look exactly as the already existing ListView.
Is there a way To use the default ListView Template in a Custom Control without rewriting it in xaml? I do have a Generic.xaml file with the new control added, but I should no need to rewrite the template code.
Thanks
EDIT: I also want to keep it as DRY as possible without repeating (making a mess) the code.
If you subclass the ListView, them your subclassed control will use the ListView Template. That's it! You do not have to do anything!
The Template used by a control is defined by its DefaultStyleKey dependency property. If you want to change the template of your control, set this property as follows:
DefaultStyleKeyProperty.OverrideMetadata(typeof(MyControl),
new FrameworkPropertyMetadata(typeof(MyControl)));
However, if you do not set this property, it will use the value set by the superclass.
I think the problem is that you have used "Add New Item" => "Custom Control" to create you control then changed the class it extends. Instead of doing this, just add a new C# class and extend ListView.
<Style TargetType="{x:Type local:MyControl}" BasedOn={StaticResource {x:Type ListView}}" />

Silverlight - preventing ChildWindow movement

Anyone got any neat solutions to prevent a Silverlight ChildWindow being moved?
thanks,
Mark
I'm not sure you'd call this neat but...
Create yourself a new Templated control and call it ImmovableChildWindow.
Modify the class it inherits from to be ChildWindow.
Open Themes/generic.xaml you will find an initial style for the ImmoveableChildWindow
In the Silverlight documentation you'll find the existing template for a ChildWindow at ChildWindow Styles and Templates.
Note the existing TargetType value for the ImmovableChildWindow style.
Copy'n' paste the whole default style for a ChildWindow from the documentation into your themes/generic.xaml file.
Replace TargetType for this copy to the same value as the exiting ImmovaleChildWindow style.
You can now delete the initial style. Leave only the large copy of ChildWindow style now targeting ImmovableChildWindow.
Find within the Template setter change the TargetType of to the same value as the style TargetType
Search through the template and find a Border with the name Chrome. Delete the x:Name="Chrome" attribute. (This is what we are really after).
Now when you create a new ChildWindow item it will by default inherit form ChildWindow, if you want it to be immovable you need modify it to inherit from ImmovableChildWindow instead (change the base type in the code-behind and the root tag name in the xaml).
The ChildWindow attaches events to the FrameWorkElement with the name "Chrome" which enables the child window to be moved about. However being a well-behaved templated control, if it can't find a FrameworkElement called "Chrome" it just continues to work without that feature.
Not Required to Create new class, instead
Copy the style from: http://msdn.microsoft.com/en-us/library/dd833070%28VS.95%29.aspx
Give x:key="stylename"
In Construtor of Childwindow, paste following code before InitializeComponent:
this.Style = App.Current.Resources["childWindow"] as Style;
above solution resolved my issue
Maybe you can try this simple way to do that:
Create a Grid to warp all the content in your ChildWindow.
<Grid Margin="0">
<!--Your ChildWindow. Canvas, Grid, Textblock...Whatever-->
</Grid>
Since the Grid has a 0 margin, you can not click it and move it.

WPF set named style elements from code behind?

I have a user control that applies a style to button, with the style containing a ControlTemplate section. Within the ControlTemplate, there are various UI elements such as an Ellipse and a Path.
If I give those elements -- the Ellipse and Path -- a name with x:Name, can I access them from code behind?
It appears the style's Ellipse and Path are not visible because I get a compile error (C#).
Am I going about this the wrong way?
Because a template can be instantiated multiple times, it's not possible to bind a generated member via x:Name. Instead, you have to find the named element within the template applied to a control.
Given simplified XAML:
<ControlTemplate x:Key="MyTemplate">
<Ellipse x:Name="MyEllipse" />
</ControlTemplate>
You would do something like this:
var template = (ControlTemplate)FindResource("MyTemplate");
template.FindName("MyEllipse", myControl);
Or even more simply:
var ellipse = (Ellipse)myControl.Template.FindName("MyEllipse", myControl);
You can read about FrameworkTemplate.FindName.
Some examples and discussion here, here and here.

Resources