I have read in two different books that in WPF, the ToolBar.Header property doesn't do anything:
Windows Presentation Foundation Unleashed by Adam Nathan, pg. 119
Pro WPF with VB 2008, pg. 650
However, I'm creating my ToolBar objects dynamically like this (tbtToolBar is actually a ToolBarTray defined in the Xaml, vm is the window's ViewModel):
foreach (IToolBarViewModel toolBarViewModel in vm.ToolBars)
{
ToolBar toolBar = new ToolBar();
toolBar.DataContext = toolBarViewModel;
// Bind the Header Property
Binding headerBinding = new Binding("Header");
toolBar.SetBinding(ToolBar.HeaderProperty, headerBinding);
// Bind the Items Property
Binding itemsBinding = new Binding("Items");
toolBar.SetBinding(ToolBar.ItemsSourceProperty, itemsBinding);
tbtToolBar.ToolBars.Add(toolBar);
}
And the Header property clearly shows up in a label as the first item in the toolbar. This is not the behavior I want. I would like to use the Header as a title in a drop-down list of ToolBars when the user right clicks on the ToolBarTray, just like the books describe.
So, I tried to get rid of the Header by setting:
toolBar.HeaderTemplate = new DataTemplate();
This works, but now there's a small unsightly gap in the toolbar.
Is there a way to make the header invisible without the gap?
Why are the books clearly wrong? Did something change between then and now?
The only way I was able to make this work was to keep the Header property null, and create another property on the ToolBarViewModel called Name.
Related
Windows Workflow Foundation 4 contains a WPF property grid similar to the one available for Winforms. The toolbar up the top has a CLEAR button rather than the X shown by Visual Studio. This control has a ControlTemplate property that is not null at runtime. The template makes use of various glyphs for toolbar buttons.
Are there any tools or techniques that will allow me to extract this template? I would like to examine and modify the XAML to restyle the toolbar (mostly just change the button label).
I have programmatic access to the template object but I'm not sure how to serialize this into XAML.
Have you tried simply using XamlWriter.Save?
e.g.
var template = control.Template;
using (var stream = new FileStream("template.xaml", FileMode.Create))
{
using (var writer = XmlWriter.Create(stream, new XmlWriterSettings() { Indent = true }))
{
XamlWriter.Save(template, writer);
}
}
have you tried Red Gate's Reflector? Of course, if the assembly is obfuscated it may not really be of much help.
I am quite new to the WPF & MVVM world, and have spent the last few days downloading as many tutorials as I could, and reading as much as possible!
I am however struggling to implement with a very basic and common concept with MVVM and am desperate for some help - and perhaps even an example :-)
I've got the basics of M-V-VM, commanding, and even messenging; but how on earch do I open a new window and set that windows ViewModel to the selected item of a listbox?
Let me explain:
I have a Model called Client and it has some properties
I then have a ViewModel which gets all my Clients and stores them in an ObservableCollection
I have a screen where I display the Name & Surname in a listbox and allow the user to filter and search.
All of the above work perfectly.
When a user selects an item though, I would like to open an editable "detailed client view" screen of that particular client. This detailed screen's ViewModel would need to somehow bind to the selected item (if that makes sense); or I need to be able to pass a paramter to the newly opened screen's ViewModel. In fact, if the user could open several detail screens at the same time and edit multiple clints it would be great!
If anyone can give me a nice example, or point me in the right direction I would truly be greatful!
Many thanks,
Brendan
I would add an event to the ListBox.SelectionChanged which does the following:
Creates a new Dialog and DialogViewModel
Binds the DialogViewModel.EditableContentProperty to the ListBox's SelectedItem
Dialog.DataContext = DialogViewModel
Dialog.ShowDialog()
Simply put:
//Create the Client Detail form
frmClientDetails frm = new frmClientDetails();
frm.Owner = this;
var ViewModel = new ClientDetailViewModel((Client)lstFoundClients.Items.CurrentItem);
frm.DataContext = ViewModel;
frm.Show();
I have idea to implement my wpf windows like TabPages in tab control. It is possible to do that dynamically in c# code.
In Example i have Menu in main window. Some Menu items calls search type windows. Is it possible to do such a thing in C# code (SomeMenuItem_Click): this code adds new tab in tabControl of main window.
If there are no search windows called -there is no tab's shown, if there are many search windows called - there are many tab's.
So how do I code this?
And whats the technique with the windows? I suppose that my search type windows must be implemented like some UserControls. I think its no a good idea to implement that like simple wpf windows. I have tried to do that by using Marlon grech "Blend like UIs using DOCKY", find at:
http://marlongrech.wordpress.com/2008/01/29/create-blend-like-uis-using-docky/
But I failed, dont find the way how to add controlls in code dynamically, not in xaml.
I would appreciate code examples to illustrate how to achieve this.
Is it possible to do such a thing in C# code (SomeMenuItem_Click): this code adds new tab in tabControl of main window.
Yes. The basic pattern is:
TabItem newItem = new TabItem();
tabControl.Items.Add(newItem);
You'll obviously need to set the relevant properties of your tab item (such as the Header and Style) but that should get you started.
You'll then need to create any controls you want to show and add them to the tab item itself (or more correctly - a container within the tab item).
I'm creating a silverlight user control that I should be able to drag and drop via blend. But this control needs to accept a map that is already on the page.
For eg.
Main.xaml contains a map control.
MapEditor.xaml contains buttons and other controls. In the .cs file, it needs to access a map control (the one in Main.xaml).
How do I go about getting this done?
I was thinking about adding a parameter in the contructor for MapEditor but how would I pass in the map as a parameter in design mode?
Thanks.
ps. I'm going to break out this control into a silverlight library so it could be used in multiple projects later.
You don't want to be giving your control a parameterised constructor, XAML will only construct types using their default constructor.
Simple Approach
The easiest approach would be to add DependencyProperty to your control to which you would assign the Map control (I'll use the type name MyMap in this example):-
public MyMap Map
{
get { return (MyMap)GetValue(MapProperty); }
set { SetValue(MapProperty, value); }
}
public static DependencyPropery MapProperty = new DependencyProperty("Map",
typeof(MyMap), typeof(MapEditor), new PropertyMetaData(null));
Now in Blend the Map property will appear in the Miscellaneous category in the Properties tab. You can then use the "Element Property" tab of the "Create Data Binding" to select the Map control to which it should bind.
Hard Core Approach
That said I would be inclined to build a proper customisable control following these guidelines Creating a New Control by Creating a ControlTemplate. With the addition that I would extend the ContentControl base class and include a ContentPresenter at the heart of the template. The control would make the assumption that the child control is a MyMap control.
This approach allows the entire appearance of the MapEditor control to be styled in Blend and it allows the Map control that is to be "edited" to be drap-drop onto the MapEditor as a child control.
I'm using a TreeView to let the user navigate a complex data structure more easily. I'm trying to add a feature to my application so my users can add new items to the datastucture by clicking a button on a toolbar. This new item has 3 levels, each with 1 item. I would like to select the item in the lowest level.
Adding the data isn't a problem, I just add a new item to the collection that is bound to the TreeView in a specific. I can lookup the item by hand browsing the TreeView, so I know the adding works. Now, I want to set the selection of the new item programmaticly. So the user can change the default settings in the element right away.
I've done some testing and I've found that setting the selection is done with something like:
var obj = TreeView.ItemContainerGenerator
.ContainerFromItem(selectedObject) as TreeViewItem;
obj.IsSelected = true;
I've tried adding this code directly after my Add-method. The adding function returns the new object and places this in selectedObject. The Add-method adds a to an ObservableCollection, which raises the appropriate events.
But, obj is always null directly after adding.
I've tried setting the selection in the LayoutUpdated event, but in this case the obj variable from the earlier code always null again.
I think I might be missing something here. Does anyone have an idea on how to add a new item to the bounded collection and select that item in the TreeView?
You might want to read this article by Josh Smith on using the treeview in WPF. He demonstrates how to use an IsSelected property that could easily be adapted for your needs, using the MVVM pattern.