Inheriting from a UserControl in WPF

Well .. you create your base control public abstract class BaseUserControl : UserControl{…} then in the XAML file : <Controls:BaseUserControl x:Class=”Termo.Win.Controls.ChildControl” xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation” xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml” xmlns:Controls=”clr-namespace:Namespace.Of.Your.BaseControl”> And that should work. EDIT: Hmm.. this example is useful when you have a base control without XAML and then inherit from it. The other way around(from a base control with … Read more

How can I tell my DataTemplate to bind to a property in the PARENT ViewModel?

The answer is this: <DataTemplate x:Key=”CodeGenerationMenuTemplate”> <MenuItem Header=”{Binding Title}” Command=”{Binding DataContext.SwitchPageCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Menu}}}” CommandParameter=”{Binding Title}”/> </DataTemplate> I just saw that Nir had given me the syntax to solve the above issue on this question: What is the best way in MVVM to build a menu that displays various pages?.

Conditional DataTemplate

Don’t set the ItemTemplate but use an ItemTemplateSelector. DataTriggers would be fine too of course, spares you the extra class for the selector. e.g. <ItemsControl.ItemTemplate> <DataTemplate> <ContentControl Content=”{Binding}”> <ContentControl.Style> <Style TargetType=”ContentControl”> <Style.Triggers> <DataTrigger Binding=”{Binding ThatProperty}” Value=”1″> <Setter Property=”ContentTemplate” Value=”{StaticResource myDataTemplate1}” /> </DataTrigger> <DataTrigger Binding=”{Binding ThatProperty}” Value=”2″> <Setter Property=”ContentTemplate” Value=”{StaticResource myDataTemplate2}” /> </DataTrigger> </Style.Triggers> </Style> </ContentControl.Style> … Read more

How does the WPF Button.IsCancel property work?

Yes, it only works on dialogs as a normal window has no concept of “cancelling”, it’s the same as DialogResult.Cancel returning from ShowDialog in WinForms. If you wanted to close a Window with escape you could add a handler to PreviewKeyDown on the window, pickup on whether it is Key.Escape and close the form: public … Read more

CommandManager.InvalidateRequerySuggested() isn’t fast enough. What can I do?

CommandManager.InvalidateRequerySuggested() tries to validate all commands, which is totally ineffective (and in your case slow) – on every change, you are asking every command to recheck its CanExecute()! You’d need the command to know on which objects and properties is its CanExecute dependent, and suggest requery only when they change. That way, if you change … Read more