background preloader

WPF

Facebook Twitter

Allowing CommandManager to query your ICommand objects. One of the great parts about commands in WPF is that they know if they can currently execute or not. When they cannot execute, the control(s) that are set to execute the command will be disabled automatically. For example, if your application has no changed data, the Save toolbar button will automatically be disabled, assuming its Command property is set to the Save command. WPF will automatically ask all of the commands being used in your UI if they can execute.

This happens at various times, such as when input focus shifts to another control, an item is selected in a list, etc. You can also programmatically trigger this to happen by calling the CommandManager’s InvalidateRequerySuggested static method. Here’s the hitch: this beautiful system of commands automatically notifying the UI that they cannot execute only works out-of-the-box for RoutedCommands. Fortunately there is an easy solution to this problem. Public void Execute(object parameter) { // Do something awesome. } } Like this: Refresh / Update WPF controls. Sometime in the past, a friend asked me how to update a control to show status while his code is doing a loop of stuff.

Essentially changing the text of a label (or sophisticatedly we can say a text-based progress bar). In my past coding with MFC and WinForms, it's fairly easy enough, you just invalidate and do an update (Invalidate / UpdateWindow in MFC or Invalidate / Update in WinForms). This approach also coincides with how Windows UI operate, where you specify the region that needs to be redrawn and then you send a message to the message pump for that control to paint itself. So, I expected something similar (if not exactly the same) to also be present in WPF; much to my surprise, there is no equivalent. So, I've decided to add a helper method to refresh a WPF control.

The LoopingMethod is just the method I use in my Window class to update the label (updating the progress) and then the code does some heavy lifting (Sleep ). Update (January 20, 2009): Update (February 26, 2009): WPFDeveloperTools - Download: WPFDeveloperTools - First Public Release. Taking snapshots of WPF animation - Eric Gunnerson's Compendium. I've been playing around with WPF animation, and ended up wanting to take a snapshot of an animation at a specific place. Specifically, I have a canvas that has some pictures on it that are moving, and I want to be able to save a bitmap of what it looks like at a specific time.

Getting the bitmap is pretty easy, using a RenderTargetBitmap and a VisualBrush. private BitmapSource CaptureScreen(Visual target){ Rect bounds = VisualTreeHelper.GetDescendantBounds(target); RenderTargetBitmap renderBitmap = new RenderTargetBitmap(800, 600, 96, 96, PixelFormats.Pbgra32); DrawingVisual dv = new DrawingVisual(); using (DrawingContext ctx = dv.RenderOpen()) { VisualBrush vb = new VisualBrush(target); ctx.DrawRectangle(vb, null, new Rect(new Point(), bounds.Size)); } renderBitmap.Render(dv); return renderBitmap;} That gives you the state of the objects pre-animation, but what I really wanted was the state of the objects during the animation. Which should have worked fine, but didn't. Data Binding in WebControls. Download source - 3.37 Kb etc) --> Introduction Over the past few months I have been creating a variety of WebControls some of which have needed access to a DataSource such as a DataSet in order to work properly.

Adding a DataSource to your WebControl is relatively easy however the tricky part is when you need to interact with that property in the Properties window e.g. in the same manner as you would for a DropDownList control etc. Now if you are lucky and you get the query right in MSDN you will come across the following article Implementing a Web Forms Data-Bound Control Designer. However you will notice like I did that the sample just does not work as advertised. While attempting to debug it I posted a message on the microsoft.public.dotnet.framework.aspnet.webcontrols newsgroup and I received the following answer from Mike Moore "DataSource property and DataBinding thread" which eventhough was not 100% what I wanted put me on the right track.

The DataSource Property Comments History. Visual Studio WPF Designer Forum. The Visual Studio WPF and Silverlight Designer Team is delighted to inform you that Visual Studio 2010 RTM is now available! Take a look at What's New for WPF and Silverlight Developers in Visual Studio 2010 ? 1. The Silverlight Designer is a fully functional drag-drop designer for Silverlight 3 RTM release out of the box, and for Silverlight 4 via an add-on (for info see 2.

The Data Sources Window works for Silverlight 3 and 43. The Property Browser has been reworked to be more usable – showing a better Category view and a "Set Properties" view4. Check out these Channel 9 videos for more info: Designing Data Driven Forms Styling Your Forms Looking ForwardFeedbackWe hope you enjoy the new release, and are really looking forward to hearing your feedback. Thanks! WPF LOB Feedback. Building a Perfect WPF Developer Workstation - Tim Sneath. Hosting a WPF Control in a Windows Forms Application - MHender Rambles On. (Blogging Tunes: Screaming Headless Torsos - "1995") We've talked about hosting Windows Forms controls in WPF applications, but what about the other way? You may very well want to just keep your existing Windows Forms application and "sprinkle" in some WPF sweetness in strategic places. That means you will need some means to be able to place WPF controls side-by-side with Windows Forms controls.

Is it do-able? Well, if it weren't, I probably wouldn't have a blog entry on the subject now would I? So what we'll do is create the converse of our other application that demonstrated hosting a Windows Forms ListBox in a WPF application. First step is to create a Windows Forms application using Visual Studio (VS): Next we'll need to add reference to the WPF namespaces and the System.Windows.Forms.Integration namespace: Note that you will need the following references: PresentationCore, PresentationFramework, UIAutomationProvider, UIAutomationTypes, WindowsBase Cool. DOH! What happened? Patterns & Practices: Composite WPF Contrib. WPF DataGrid Practical Examples. Download source code - 65.44 KB Contents Introduction A DataGrid is a user interface component for displaying tabular data to the user, typically providing sorting and editing functionality, among others.

DataGrids have been the work-horse of various frameworks such as ASP.NET (GridView) and Windows Forms (DataGridView). However, DataGrids in WPF are rather conspicuous in their absence! Fortunately, the absence of this control has not hampered the popularity of WPF. Eventually, in August 2008, Microsoft released its DataGrid CTP (Community Technology Preview - a public beta) to CodePlex to coincide with the release of the .NET Framework 3.5 SP1 and Visual Studio 2008 SP1. More recently, on October 22 2008, DataGrid v1 was released. Article Overview Currently, there is a lack of documentation and examples demonstrating common DataGrid usage scenarios (which is to be expected, as it is an out-of-band release). I cannot, of course, cover everything. Installing the WPF Toolkit Performing Updates. ChartingStylingGuide.pdf (application/pdf Object)

Styling Microsoft’s WPF datagrid - Jaime Rodriguez. Microsoft’s WPF datagrid has a lot of properties and styles you can tweak to get it looking right (if you are a designer). Below, find my cheat sheet to styling the grid. It is not 100% comprehensive but it gets you far and has a few very useful tips & gotchas. At the highest level in the DataGrid , you can change the look & feel by setting some of these: Here, you can see a visual representation for a few of these properties (the visual is not all inclusive); this will give you an idea of what this article will cover. Backgrounds: The interesting part are the relationships amongst the backgrounds: Background – sets the whole data grid’s background.

Notice it can be any brush, solid and gradients is obvious, but why not a DrawingBrush like the bear above ( which you can see if you squint hard, it has Opacity =0.1) RowBackground and AlternatingRowBackground set the background for a row and alternating row. Notice that, on purpose, I only override AlternationIndex = 2,3. Cell Styles. A WPF Problem Solved Two Very Different Ways - Using XAML Only - Using a Custom Control. Introduction This article is about developing a XAML only WPF CheckListBox and ListBox with Selection Indicator control styles, and taking the same requirements and building a custom control that takes a very different approach to the problem. This article is about taking a concept and running with it. It's about this developer's desire to recreate a complex control using only XAML markup.

We will explore the XAML only approach, then toss everything into a blender, and pour out two WPF Custom Controls using VB.NET. This article and its controls were inspired by Josh Smith's awesome article, The WPF Thought Process. It's with Josh's permission and encouragement I write this article and post it here. I read Josh's article several times, attempting to glean and learn as I do from all the WPF articles here on The Code Project.

To get the most from this article, please read Josh's article before reading this one. I've only been programming WPF since April 2007. My Expedition I love challenges. WPF Binding Cheat Sheet. WPF Binding Cheat Sheet « Method ~ of ~ Tried. Download. WPF Enum List Converter. Introduction This article demonstrates how to bind a C# enum definition to a WPF ComboBox, which is not as simple as it first seems. The solution was extracted from a real-world commercial application. Background Enum definitions are great for defining small fixed lists of options for a property. They are easy to define in code, and are strongly typed, so they help produce robust code. Unfortunately, these advantages become disadvantages when working with WPF, as enums are not suitable for XAML. My data models tend to use a lot of enums to define class specific properties.

I did not want to reference the combobox in the code-behind class, I wanted the bindings to be entirely defined on the XAML side. Using the Code The solution to the enum problem is implemented in two small C# classes described below. The source code contains everything needed to build the example, including the test data, which is taken from the SQL Server AdventureWorks sample database. How it Works Conclusion. Drag & drop in WPF part2 ... - Jaime Rodriguez. Last part, we focused on the drag... In this part we will focus on drop (which is relatively straight forward) ... DropTargets must set AllowDrop = true It all begins with a Visual Element setting its AllowDrop to true; this tells the D&D subsystem your control is a drop target ...

You can do this in XAML or in code.. DragEnter, DragOver, DragLeave I hardly ever subscribe to any of these events.. but let's walk through the niceties on each .. DragEnter -- happens when cursor/mouse enters; useful for things like "highlighting" the target by showing an adorner, or similar ... Walking through the Drop code with an example: From our previous work in progress project, I replaced old DropTarget StackPanel with a DropTargetText and DropTargetImages ...

The source code for snippet above is here. Handling the Drop can be that straight forward, but it almost never is that simple.. In Part3, I can walk through much more samples of handling drop ...