What are Attached Properties and how to register it?
An ‘attached’ property is a concept defined by Extensible Application Markup Language (XAML). It is intended to be used as a type of global property that can be set on any object. In Windows Presentation Foundation (WPF), attached properties are typically defined as specialized forms of dependency properties that do not have the conventional ‘wrapper’ property. This example shows how to ’register’ an attached property and provide public accessors so that you can use the property in both Extensible Application Markup Language (XAML) and code. Attached properties are a syntax concept defined by Extensible Application Markup Language (XAML). Most of the attached properties for WPF types are also implemented as dependency properties. You can use dependency properties on any ’DependencyObject’ types. [C#] public static readonly DependencyProperty IsBubbleSourceProperty = DependencyProperty.RegisterAttached( ‘IsBubbleSource’, typeof(Boolean), typeof(AquariumObject), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender) ); public static void SetIsBubbleSource(UIElement element, Boolean value) { element.SetValue(IsBubbleSourceProperty, value); } public static Boolean GetIsBubbleSource(UIElement element) { return (Boolean)element.GetValue(IsBubbleSourceProperty); }
How do I view the XAML resources compiled into an assembly in BAML format?
Embedded XAML Resources Several WPF assemblies come with embedded XAML resources, like template definitions, style definitions, etc. And it’s often very useful to take a look at these embedded XAML to understand the structure of a control and thereby making it easier to customize it’s UI in your applications, if necessary. The best way to take a look at these embedded XAML resources is to use the Reflector tool with the BAML Viewer plugin. Reflector + BAML Viewer Reflector is a popular Free tool available since the early days of .NET to help you take a deeper look at the source and resources comprising a .NET assembly. Reflector can be downloaded here: .NET Reflector Here is a thumbnail view of the Reflector tool: Once you install the Reflector, download the BAML Viewer plug-in from: BAML Viewer – Reflector Plugin The plug-in is a single dll called Reflector.BamlViewer.dll. Put this dll next to the Reflector.exe and follow these steps to add the plug-in to the Reflector: a) In the Reflector tool, select the View/Add Ins… menu item. b) In the ‘Add Ins’ dialog box, Add the above dll as an ‘add-in’. This will include a new ‘Tools/BAML Viewer’ menu item in the Reflector. c) Then open a WPF assembly (or Syncfusion assembly) that usually contains a BAML resource, like PresentationFramework.Aero.dll (usually found under ‘%ProgramFiles%\Reference Assemblies\Microsoft\Framework\v3.0\PresentationFramework.Aero.dll’) d) Then select the ‘Tools/BAML Viewer’ menu item. This will open a new ‘BAML Viewer’ view showing the embedded XAML in the above assembly. Here is a screenshot of the BAML Viewer showing the embedded XAML in the PresentationFramework.Aero.dll: You can similarly view XAML resources embedded into Syncfusion assemblies.
How do you bind the available system fonts to a combobox and bind to the selection?
You can do so as follows: <ComboBox Name=’fontCombo’ ItemsSource='{x:Static Fonts.SystemFontFamilies}’ /> <!–And bind the TextBlock.FontFamily to the selected item of the above combo as follows –> <TextBlock Text=’Something’ FontFamily='{Binding ElementName=fontCombo, Path=SelectedItem}’ />
How do I show tooltips for the listview items?
You can do so by first defining a DataTemplate for the corresponding columns as follows. Sample code can be seen with better formatting in this thread (ListView Column ToolTip) [XAML] <Page xmlns=’http://schemas.microsoft.com/winfx/2006/xaml/presentation’ xmlns:x=’http://schemas.microsoft.com/winfx/2006/xaml’> <StackPanel> <StackPanel.Resources> <DataTemplate x:Key=’nameTemplate’> <TextBlock Text='{Binding Name}’ Margin=’-6,0′> <TextBlock.ToolTip> <ToolTip> <TextBlock Foreground=’Green’ Text='{Binding Name}’/> </ToolTip> </TextBlock.ToolTip> </TextBlock> </DataTemplate> <DataTemplate x:Key=’namespaceTemplate’> <TextBlock Text='{Binding Namespace}’ Margin=’-6,0′> <TextBlock.ToolTip> <ToolTip> <TextBlock Foreground=’Green’ Text='{Binding Namespace}’/> </ToolTip> </TextBlock.ToolTip> </TextBlock> </DataTemplate> </StackPanel.Resources> <ListView> <ListView.ItemContainerStyle> <Style TargetType=’ListViewItem’> <Setter Property=’HorizontalContentAlignment’ Value=’Stretch’/> <Setter Property=’VerticalContentAlignment’ Value=’Stretch’/> </Style> </ListView.ItemContainerStyle> <ListView.View> <GridView> <GridViewColumn CellTemplate='{StaticResource nameTemplate}’ Width=’300′ Header=’Name’/> <GridViewColumn CellTemplate='{StaticResource namespaceTemplate}’ Width=’300′ Header=’Namespace’/> </GridView> </ListView.View> <x:Type TypeName=’Visual’/> <x:Type TypeName=’FrameworkElement’/> <x:Type TypeName=’FrameworkContentElement’/> </ListView> </StackPanel> </Page>
How do I obtain a reference to the child TreeViewItems of a parent TreeViewItem?
It’s a bit tricky because WPF doesn’t ‘realize’ the child nodes until they get rendered and you will also have to use some not very straight forward APIs to obtain the child. So, you will have to do something like this: // First set IsExpanded to true. tviParent.IsExpanded = true; // Then wait for the child items to get generated if (tviParent.ItemContainerGenerator.Status != System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated) tviParent.ItemContainerGenerator.StatusChanged += new EventHandler(TempItemContainerGenerator_StatusChanged); else // If child items are already generated this.OnReadyToSelect(); private void OnReadyToSelect() { // Here you can obtain and select any child of the currently selected TreeViewItem using it’s ItemContainerGenerator. TreeViewItem childItem = tviParent.ItemContainerGenerator.ContainerFromItem(collectionItem); //Or specify an index. childItem.IsSelected = true; }