Thursday, February 19, 2009

Pandora WTF

I was sitting here jamming out to some Pandora this morning, and wondered what song I was hearing, so I flipped over to my desktop to see what was playing and I see this:

PandoraPanteraWTF

That’s the cover for Pantera’s Reinventing the Steel album??

Tuesday, February 10, 2009

Responsive WPF Interfaces

I had a bit of a challenge today while trying to get a WPF ProgressBar to send feedback to my UI while a long-running processes ran. 

My first challenge was finding “that one property that lets the ProgressBar animation run continuously” – IsIndeterminate

My next challenge was setting the IsIndeterminate property to true before firing off my long-running process.   I remembered something about using the Dispatcher, but even using that didn’t reflect the ProgressBar property changes to the UI.  I briefly considered trying an evil DoEvents type trick, but came to my senses before heading down that road.

I landed on creating a delegate which I could invoke to fire off my long-running process, and it ended up looking something like this (forgive the crude method names, this was some spike code I was playing around with:

private delegate void UpdateProgressbarIndeterminateState(bool isEnabled);
private void CompareClick(object sender, RoutedEventArgs e){
Dispatcher.Invoke(DispatcherPriority.Normal,
(UpdateProgressbarIndeterminateState)
delegate(bool isIndeterminate){
progressBar.IsIndeterminate = isIndeterminate;
}, true);

AsyncDelegate caller = PerformCompare;
caller.BeginInvoke(AfterCompareCallback, null);
}

internal delegate void AsyncDelegate();
private static void PerformCompare() {
DataComparer.Execute();
}

protected void AfterCompareCallback(IAsyncResult ar) {
Dispatcher.Invoke(DispatcherPriority.Normal,
(UpdateProgressbarIndeterminateState)
delegate(bool isIndeterminate) {
progressBar.IsIndeterminate = isIndeterminate;
}, false);
}

Tuesday, February 03, 2009

WPF DataGrid Conditional Formatting

I recently ran into a problem on my project where I needed to apply some conditional formatting to WPF DataGrid Cells based upon values from another property in my bound object.

The requirement was pretty simple: if condition is met, change the cell-text color to red, otherwise, leave it black.  What should have been a pretty simple task (never heard a programmer say that, right?) ended up taking me more time than I’d care to admit.

After fumbling around with a few attempts at the IValueConverter and a DataTemplateSelector, I still was not having any luck in getting my conditional formatting to work.  I eventually landed back at my usual source of information for the WPF ToolkitCodePlex, and “shockingly”, the answer was in the discussion for the project.

I had been fighting the “Foreground” property of the DataGridTextColumn directly, trying what was recommended in this article, but I just couldn’t get that working.  Once I followed the CodePlex forum recommendation of embedding a CellStyle into the DataGridTextColumn, things worked as expected.