Where do I catch Exceptions in MVVM?

I’ve been handling exceptions in my MVVM client by catching them and wrapping them in an ErrorViewModel property of whatever ViewModel caught the exception. Let’s say a ViewModel A catches the EndpointNotFoundException. To present this error, I wrap the Exception in an ErrorViewModel and assign that to A‘s Error property. The View associated with A … Read more

Ambiguous type reference. A type named `VisualState` occurs in at least two namespaces

This error(most of the time warning) will occur when using two or more references which contains same namespace and classes. in your case you are using VisualState which is part of PresentationFramework assembly and you might have added another assembly which contains same “VisualState” object with the same namespace “System.Windows” . you can resolve the … Read more

What is the Difference Between x:Key, x:Name, and x:UID in a DataTemplate in WPF?

The ‘x:’ specifies the namespace, which would in your case most likely be “http://schemas.microsoft.com/winfx/2006/xaml” You will see the alias declared at the top of your Window.Xaml file. x:Key, x:Name, etc are all directives in that namespace. In contrast, the ‘Name’ attribute (without the x:) is a dependency property declared in the FrameworkElement class. x:Key Uniquely … Read more

WPF VirtualizingStackPanel for increased performance

There are two techniques that might be a big help. Both of them are described very well by Bea Stolnitz on her blog. The first is UI Virtualization and the second is Data Virtualization In UI virtualization you use things like VirtualizingStackPanel to make the UI draw fewer things. Data virtualization makes sure you don’t … Read more

WPF: TabControl & DataTemplates

First of all, there are two templates involved here: TabControl.ItemTemplate, used to render the TabItem headers TabControl.ContentTemplate, used to render the TabItem contents If you don’t set these properties explicitly then WPF will attempt to resolve them elsewhere. It will walk up the logical tree looking for a resource telling it how to render your … Read more

How to point one resource (a SolidColorBrush) at another

I agree with what Rachel said, but if you have to base it on an existing SolidColorBrush, you can do it with the following: <SolidColorBrush x:Key=”MyDataGridHeaderBrush” Color=”{Binding Source={StaticResource HeaderBrushDefinedElsewhere}, Path=Color}”/> Note this just works for the “Color” attribute, you’d have to do it separately for each attribute you needed.

How to display row numbers in a ListView?

I think you have the elegant solution, but this works. XAML: <ListView Name=”listviewNames”> <ListView.View> <GridView> <GridView.Columns> <GridViewColumn Header=”Number” DisplayMemberBinding=”{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListViewItem}}, Converter={StaticResource IndexConverter}}” /> <GridViewColumn Header=”Name” DisplayMemberBinding=”{Binding Path=Name}” /> </GridView.Columns> </GridView> </ListView.View> </ListView> ValueConverter: public class IndexConverter : IValueConverter { public object Convert(object value, Type TargetType, object parameter, CultureInfo culture) { ListViewItem item … Read more

Infinitely rotate rectangle in XAML

Something like this <Rectangle x:Name=”rect1″ RenderTransformOrigin=”0.5, 0.5″> <Rectangle.RenderTransform> <!– giving the transform a name tells the framework not to freeze it –> <RotateTransform x:Name=”noFreeze” /> </Rectangle.RenderTransform> <Rectangle.Triggers> <EventTrigger RoutedEvent=”Loaded”> <BeginStoryboard> <Storyboard> <DoubleAnimation Storyboard.TargetProperty=”(Rectangle.RenderTransform).(RotateTransform.Angle)” To=”-360″ Duration=”0:0:1″ RepeatBehavior=”Forever” /> </Storyboard> </BeginStoryboard> </EventTrigger> </Rectangle.Triggers> </Rectangle> Of course you can remove Loaded trigger and run this storyboard whenever you … Read more

How to create WPF usercontrol which contains placeholders for later usage

ContentControls & ItemsControls are good for this, you can bind them to a property of your UserControl or expose them. Using a ContentControl (for placeholders in multiple disconnected places): <UserControl x:Class=”Test.UserControls.MyUserControl2″ xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” xmlns:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006″ xmlns:d=”http://schemas.microsoft.com/expression/blend/2008″ Name=”control”> <Grid> <Button>Just a button</Button> <ContentControl Content=”{Binding PlaceHolder1, ElementName=control}”/> </Grid> </UserControl> public partial class MyUserControl2 : UserControl { public static … Read more