CodeRelief.net
Posts Tagged Styles
WPF: DataContext for ContextMenu
Posted by Tim Valentine in Bug Patches, Controls, Customization, WPF on April 20, 2011
Problem
Often times WPF developers want to throw their computer out the window because of a small and very irritating truth about the ContextMenu control. I’m talking about the fact that ContextMenu is not in the same visual tree as its containing parent, resulting in many data binding issues. Since the ContextMenu is not in the same visual tree, ElementName, RelativeSouce (FindAncestor), etc bindings will not work. You can get around this through the use of the PlacementTarget property and some complex DataContext re-routing, but it is a pain, confusing, and does not scale well (at all). I will present a very simple attached property that relieves the situation.
Solution
Attached Properties, C#, ContextMenu, Controls, Style, Styles, WPF, XAML
WPF: DataGrid ContextMenu for Column Visibility
Posted by Tim Valentine in Controls, Customization, WPF on January 11, 2011
Introduction
When dealing with software applications it is commonplace for users to have certain expectations. These expectations not only include the abilities of an application, but also include how such abilities are executed. The specific expectations that this article is focused upon directly concern those of the data grid control, and more specifically Microsoft’s implementation of such a control under the Windows Presentation Foundation (WPF) user-interface rendering sub-system for Windows-based applications.
Users have come to expect the ability to hide or show the columns of a data grid control. Such an action is typically executed by right-clicking on any of the data grid’s column headers. This results in the option to select a menu item, via a context menu, that will toggle the visibility of the column labeled on the menu item. Therefore providing this ability with access to execute its associated actions in the typical way, would be a wise and logical decision. The implementation of meeting this need should be provided through an encapsulated and reusable interface. Unfortunately, Microsoft’s implementation of the data grid control does not offer this functionally as a built-in feature and adding it has proving to be a difficult task.
In this article you will be provided with a solution that implements this functionality specifically for Microsoft’s implementation of the data grid control. This solution has proving reliable regardless of whether the data grid’s columns have been auto-generated through the data in which it represents, while still conforming to the generally accepted Model-View-ViewModel (MVVM) design pattern guidelines. This has been accomplished by utilizing WPF’s attached properties system whose means provide the ability of separate child elements to specify unique values on properties defined in a parent element; thus providing the ability to manipulate the parent element’s behavior indirectly. In this way the concepts of encapsulation and reuse have been applied.
Attached Properties, C#, ContextMenu, Controls, DataGrid, DataGridColumn, DataGridColumnHeader, Hide, MenuItem, Show, Style, Styles, WPF, XAML
WPF: DataGrid Select-all-button Styling
Posted by Tim Valentine in Controls, Customization, WPF on January 4, 2011
Problem
The SelectAll button on Microsoft’s DataGrid control cannot be styled without a workaround. This is a direct result of there being no SelectAllButtonTemplate property on the control. To get around this issue we’ll make our own property using attached properties. This way we can have nice, clean, MVVM-compliant code and XAML-markup, while still getting what we want.
Solution
Attached Properties, Button, C#, Controls, DataGrid, SelectAllCommand, Style, Styles, WPF, XAML
WPF: Non-selectable ListBoxItem and ListViewItem
Posted by Tim Valentine in Controls, Customization, WPF on January 4, 2011
Problem
Sometimes in WPF you want to have a list of items contained in a ListBox (or ListView) that you don’t want to be selectable. You could set those items to be disabled, by assigning IsEnabled to False, but that may interfere with styles among other things. You could also (probably the best idea) use a ItemsControl, from which the ListBox derives, that doesn’t include selectable-items functionality. However, for whatever reason, you may just want to use a ListBox (or ListView) instead.
Solution
Attached Properties, C#, Controls, ListBox, ListBoxItem, ListView, ListViewItem, Styles, WPF, XAML
Auto-Select First Item Using XAML for Selector-Derived Controls (ListBox, ListView, TabControl, etc)
Posted by Tim Valentine in WPF on July 9, 2009
Problem
When your application has a control on it that derives from Selector and has no items on load nothing is selected. During run-time when the user is clicking around and adds items to the Selector-derived control, you would assume that the control would automatically select the first item because that only makes sense. However, your assumption and mine would be wrong; this is not the behavior at all. Instead the selector will still have no items selected. This means in order to get this “auto-select first item” behavior, you have to do it yourself. Luckily its an easy fix as shown below.
Categories
- General (6)
- VSTO (1)
- WPF (11)
- Controls (6)
- Bug Patches (3)
- Customization (4)
- Controls (6)
- Access Keys AccessKeysManager Animation Arrays Attached Properties Bug Patch Button C# Casting Code Generation COM ContextMenu Controls DataGrid DataGridColumn DataGridColumnHeader Dynamic Dynamic Classes Exception Frame-based Hashtable Hide ListBox ListBoxItem ListView ListViewItem Maximize MenuItem MessageBar Minimize MVC Run-time Scope Scoping SelectAllCommand Selector Show Style Styles VSTO Window WindowCustomizer WindowHelper Windows Forms WinForms WPF XAML
Archives
- June 2014 (1)
- July 2012 (2)
- March 2012 (2)
- November 2011 (1)
- August 2011 (1)
- April 2011 (1)
- January 2011 (3)
- April 2010 (1)
- March 2010 (1)
- February 2010 (1)
- July 2009 (2)
- June 2009 (1)
- May 2009 (1)