WPF Image Control with Click Event

To expand on Shawn Mclean’s answer, one of the great capabilities of WPF is the ability to leverage the behavior of a control while completely changing the look of that control. If you want to create an image that behavior just like a button (complete with click events, command binding, default button assignment, etc.) you … Read more

Is there a way to determine where a WPF Binding is declared/created?

When debugging WPF binding errors, I find it easiest to break up the error by the semicolons, and start from the end System.Windows.Data Error: 4 : Cannot find source for binding with reference ‘RelativeSource FindAncestor, AncestorType=”System.Windows.Controls.ItemsControl”, AncestorLevel=”1″‘. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is ‘MenuItem’ (Name=””); target property is ‘HorizontalContentAlignment’ (type ‘HorizontalAlignment’) So starting from the end: … Read more

WPF ListBoxItem double-click?

It is possible to bind commands with parameters to ListBoxItems without using code-behind or attached behaviors, simply by using InputBindings with a MouseBinding, as shown before in this answer. Example ListBox with MouseBinding for LeftDoubleClick: <ListBox ItemsSource=”{Binding MyDataSource}”> <ListBox.ItemTemplate> <DataTemplate> <TextBlock Text=”{Binding MySourceItemName}”> <TextBlock.InputBindings> <MouseBinding MouseAction=”LeftDoubleClick” Command=”{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}” CommandParameter=”{Binding MySourceItemId}” /> </TextBlock.InputBindings> … Read more

The name ViewModel does not exist in the namespace “clr-namespace:Project.ViewModels”

clr-namespace:MyProject.ViewModels There has to be a namespace called MyProject.ViewModels that has a class Called MainPageViewModel that is public and has a public parameterless constructor within the same assembly as ProjectDatabaseRebuilder.MainWindow. There is not. If MyProject.ViewModels exists in a referenced assembly, you must state so within the xmlns. xmlns:vm=”clr-namespace:MyProject.ViewModels;assembly=MyProject” or some such. Honestly, it looks like … Read more

One sentence explanation to MVVM in WPF?

One sentence explanation: MVVM is a reimagining of the well loved Model-View-Presenter (MVP) pattern that is designed to work especially well with databinding facilities supplied with WPF to separate application logic from UI design. Longer, more useful, explanation: The basic concept of MVVM is the break apart a WPF application into separate components each of … Read more

WPF ControlTemplate: How to provide a default value for TemplateBinding?

You can just define setters on your style for the two properties in question. For example, some general definitions: <LinearGradientBrush x:Key=”ButtonNormalBackground” EndPoint=”0,1″ StartPoint=”0,0″> <GradientStop Color=”#F3F3F3″ Offset=”0″/> <GradientStop Color=”#EBEBEB” Offset=”0.5″/> <GradientStop Color=”#DDDDDD” Offset=”0.5″/> <GradientStop Color=”#CDCDCD” Offset=”1″/> </LinearGradientBrush> <SolidColorBrush x:Key=”ButtonNormalBorder” Color=”#FF707070″/> Then, in your style definition: <Setter Property=”Background” Value=”{StaticResource ButtonNormalBackground}” /> <Setter Property=”BorderBrush” Value=”{StaticResource ButtonNormalBorder}” />

Strange Error – CS0012: The type x is defined in an assembly that is not referenced

I’m Mike’s coworker, and we worked out a solution. The type X is defined in his assembly, that is only in the GAC. Even though his ASP.NET web appplication did have a reference, it was failing to load from the GAC only for this UserControl. The rest of the application worked as expected. We confirmed … Read more

How do I dynamically generate columns in a WPF DataGrid?

Ultimately I needed to do two things: Generate the columns manually from the list of properties returned by the query Set up a DataBinding object After that the built-in data binding kicked in and worked fine and didn’t seem to have any issue getting the property values out of the ExpandoObject. <DataGrid AutoGenerateColumns=”False” ItemsSource=”{Binding Results}” … Read more