Default MenuItem TopLevelHeader Control Template - wpf

Where can I get the MenuItem TopLevelHeader Control Template? The MSDN link for styling menu items gives a modified template.
I need to obtain a control template that contains a default pop-up/context menu.

Manny tools are available that will serve the purpose like stylesnooper and Show Me The Template
but if you have Microsoft Expression Blend you can extract the default control template by
Draggint the control onto the design
surface
Right click the control and choose
Edit Template -> Edit Copy
When you do this, Blend will extract the base template from the control and explicitly declare it within document/application as a resource which you can then edit to your liking.
Check this for more
http://www.shafqatahmed.com/2009/01/wpf-kid-stuff-extracting-a-control-template.html

I could not get Blend or the usual tools to access that control template but you can extract it yourself with code like the following:
var controlTemplate = (ControlTemplate)FindResource(MenuItem.TopLevelHeaderTemplateKey);
var sb = new StringBuilder();
var xml = XmlWriter.Create(sb, new XmlWriterSettings { Indent = true, NewLineOnAttributes = true });
XamlWriter.Save(controlTemplate, xml);
var xaml = sb.ToString();
Debug.WriteLine(xaml);
The output is too long to include here.

Related

WPF Datagrid Button commandParameter in code behind

I'm trying to build a datagrid with columns and a button in code behind and I want to assign the command parameter of this button to the value of the "ID" column which is "TDENT_ID". Here's my code
this line is not working as you can see :
BtnDetail.SetBinding(System.Windows.Controls.Primitives.ButtonBase.CommandParameterProperty, new Binding() { Source = dtGrid, Path = "TDENT_ID" });
How can I write it?
FrameworkElementFactory:
"This class is a deprecated way to programmatically create templates, which are subclasses of FrameworkTemplate such as ControlTemplate or DataTemplate; not all of the template functionality is available when you create a template using this class. The recommended way to programmatically create a template is to load XAML from a string or a memory stream using the Load method of the XamlReader class." Microsoft Docs
Your Bindingis wrong. It currently points to the DataGrid which does not have a property TDENT_ID.
This property is on your data item, which is the DataContext of the column's template.
Therefore, correct Binding would be:
// Set up a Binding that uses the current DataContext as Binding.Source
BtnDetail.SetBinding(ButtonBase.CommandParameterProperty, new Binding(nameof(myItemType.TDENT_ID)));
Microsoft Docs: Specifying the binding source
Try to learn XAML. It makes writing such UI related code much easier, which makes you more productive.

MahApps:Metro SplitButton Control Template

Default SplitButton works ok, but when I try to work with it's control template issues arise.
If I try to get Control Template with reflection (with ConstructorInfo) I get empty Control Template for SplitButton. If I try to 'Edit Template copy' in XAML Designer, I get copy which does not work (like ItemsSource does not bind to elements in ListBox of SplitButton as it is always empty).
My version of MahApps Metro is 1.4.3.0
Here is how I try to get Control Template of SplitButton:
MahApps.Metro.Controls.SplitButton ctl = sender as MahApps.Metro.Controls.SplitButton;
Type type = ctl.GetType();
if (type == null)
return;
// Instantiate the type.
ConstructorInfo info = type.GetConstructor(System.Type.EmptyTypes);
Control control = (Control)info.Invoke(null);
// Get the template.
ControlTemplate template = control.Template;
// Get the XAML for the template.
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb, settings);
XamlWriter.Save(template, writer);
The default ControlTemplate is available on GitHub: https://github.com/MahApps/MahApps.Metro/blob/336f7dfc4bda2d0eba8aa270737ca3c11d45128c/src/MahApps.Metro/MahApps.Metro/Themes/SplitButton.xaml
MahApps.Metro is open source so you can download the source code if you want to.
After updating MahApps Metro to 1.5.0 SplitButton works fine with provided Control Templates...

Create Silverlight custom control with code only (no xaml)

I would like to create a Silverlight custom control using C# only, without any xaml.
Here is my work so far (stripped down to the bare minimum for the question):
I tried to inherit User control as follows:
public class myControl: UserControl
{
// class code
}
And adding it to the LayoutRoot:
myControl control = new myControl();
LayoutRoot.Children.Add(control);
The control is added, but its invisible!!
How can i make it visible ? Is there something i missed ?
edit: The only visual element in my contorl is a grid with an image background
Your Usercontrol will be empty and have no visual effect until you give it a child control via it's Content property.
Well unless you put a template in place or add elements in code, UserControl is empty.
Maybe you could try inheriting from an existing control which has a template, like Button, etc and change that in code?

Silverlight Windows Phone 7: How to add content to a PivotControl?

I have a pivot control:
PivotItem sectionPivot = new PivotItem()
{
Header = sect.Name,
Content = new StackPanel()
};
How can I add content to it?
Edit: The reason I'm doing it programmatically is because I don't know how many pivots there will be or what they will contain until runtime. Is there some way to do that in XAML?
If you're going to do it all programatically, just add stuff to the stack panel you just created.
var panel = new StackPanel();
panel.Children.Add(new TextBlock() { Text = "Hello" });
PivotItem sectionPivot = new PivotItem()
{
Header = sect.Name,
Content = panel;
};
I typed all that without doing any checking, but hypothetically that should work...
Another answer from me. The OP added info to the question that they don't know how many there could be, and if you could still do it in XAML.
Yes, you can.
The Pivot control has an ItemsSource property, and you could bind that to something in your class that is being populated dynamically.
<controls:Pivot Title="MY APPLICATION" ItemsSource="{Binding MyPivotItemsSource}" />
Each item in that source would end up as a pivotitem. You'd also have to set up templates and stuff, so its still a lot of work...

Dynamic Control Template in Silverlight

I'm trying to make a button in Silverlight use a control template to change the way it looks.
I need to do this dynamically in code (not xaml markup).
The Button object has a Template property to which you can assign a ControlTemplate.
But how do you stuff UI elements into the ControlTemplate?
(In WPF, there is a VisualTree property but no such property exists in Silverlight)
I'm not sure if this helps, but just in case. To create buttons using a control template in code behind (not XAML) I've done it like this:
load the control template from an xml definition (below is a link to the source)
byte[] bytes = ReadBytesFromStream("BestBuyRemix.BL.buttontemplate.xml");
string buttonTemplate = "";
UTF8Encoding encoding = new UTF8Encoding();
buttonTemplate = encoding.GetString(bytes.ToArray(), 0, (int)bytes.Length);
create the button and add it to the visual tree (in this case a wrap panel)
string onebutton = string.Format(buttonTemplate, mnu.CatItemName, mnu.CatItemImage,
"{StaticResource buttonStyle1}",
"{StaticResource CatItemNameBlock}", "{StaticResource ThumbNailPreview}",
ictr.ToString());
ictr += 1;
Button bt = (Button)XamlReader.Load(onebutton);
bt.Tag = mnu.CatItemPageUri;
bt.Click += new RoutedEventHandler(bt_Click);
Wrappable.Children.Add(bt);
I wrote a post on my blog about the Best Buy Remix API which uses this to build a product list in the details page. It has a link to the Silverlight source. In case you're interested.
blog post link

Resources