How can I turn binding errors into runtime exceptions?

You could hook into the PresentationTraceSources collection with your own listener: public class BindingErrorListener : TraceListener { private Action<string> logAction; public static void Listen(Action<string> logAction) { PresentationTraceSources.DataBindingSource.Listeners .Add(new BindingErrorListener() { logAction = logAction }); } public override void Write(string message) { } public override void WriteLine(string message) { logAction(message); } } and then hook it … Read more

WPF Binding to local variable

The pattern is: public string Text {get;set;} and the binding is {Binding Text, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}} If you want the binding to update automatically you should make it a DependencyProperty. I think 3.5 added ElementName to bindings, so the following is a little easier: <Window x:Name=”Derp” … <TextBlock Text=”{Binding Text, ElementName=Derp}”/>

WPF – Making hyperlinks clickable

You need something that will parse the Text of the TextBlock and create the all the inline objects at runtime. For this you can either create your own custom control derived from TextBlock or an attached property. For the parsing, you can search for URLs in the text with a regular expression. I borrowed a … Read more

VirtualizingStackPanel + MVVM + multiple selection

I found another way of handling selection in the MVVM pattern, which solved my issue. Instead of maintaining the selection in the viewmodel, the selection is retrieved from the ListView/ListBox, and passed as a parameter to the Command. All done in XAML: <ListView x:Name=”_items” ItemsSource=”{Binding Items}” … /> <Button Content=”Remove Selected” Command=”{Binding RemoveSelectedItemsCommand}” CommandParameter=”{Binding ElementName=_items, … Read more

XAML Combine styles going beyond BasedOn?

You can make a custom markup extensions that will merge styles properties and triggers into a single style. All you need to do is add a MarkupExtension-derived class to your namespace with the MarkupExtensionReturnType attribute defined and you’re off and running. Here is an extension that will allow you to merge styles using a “css-like” … Read more

WPF ListView with horizontal arrangement of items?

It sounds like what you are looking for is a WrapPannel, which will lay the items out horizontally until there is no more room, and then move to the next line, like this: (MSDN) alt text http://i.msdn.microsoft.com/Cc295081.b1c415fb-9a32-4a18-aa0b-308fca994ac9(en-us,Expression.10).png You also could use a UniformGrid, which will lay the items out in a set number of rows … Read more

Select TreeView Node on right click before displaying ContextMenu

Depending on the way the tree was populated, the sender and the e.Source values may vary. One of the possible solutions is to use e.OriginalSource and find TreeViewItem using the VisualTreeHelper: private void OnPreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) { TreeViewItem treeViewItem = VisualUpwardSearch(e.OriginalSource as DependencyObject); if (treeViewItem != null) { treeViewItem.Focus(); e.Handled = true; } } … Read more

How to reference image resources in XAML?

If the image is in your resources folder and its build action is set to Resource. You can reference the image in XAML as follows: “pack://application:,,,/Resources/Search.png” Assuming you do not have any folder structure under the Resources folder and it is an application. For example I use: ImageSource=”pack://application:,,,/Resources/RibbonImages/CloseButton.png” when I have a folder named RibbonImages … Read more