Debugging XAML Binding

Good tip of the day:

To debug XAML Binding you can add reference to Diagnostics from WindowsBase dll to the XAML file, 
then when binding to some property add PresentationTraceSources.TraceLevel. When you run it check out 
the output window.

<TextBlock Text="{Binding someProperty, diagnostics:PresentationTraceSources.TraceLevel=High}"/>


Binding multiple properties to one XAML element using WPF MultiBinding

     <GridViewColumn Header="Address">
               <MultiBinding StringFormat="{}{0}, {1}">
                 <Binding Path="Address.City"/>
                 <Binding Path="Address.State"/>


ListView SelectedItem color and Alternate row background

Lots of examples on how to change ListView SelectedItem color will overwrite the System.Colors.HighlightBrushKey and SystemColors.ControlBrushKey:
<Style x:Key="myListboxStyle">
        <!-- Background of selected item when focussed -->
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Red" />                
        <!-- Background of selected item when not focussed -->
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Green" />
You can argue whether that's elegant or not, I just didn't want to do that, so below is a slightly different aproach: I create my own element (Border in this case) and change it's color. Also jammed into here alternate background triggers.
<ListView ItemContainerStyle="{StaticResource ListViewItemStyle}"
          AlternationCount="2" />

<Style x:Key="ListViewItemStyle" TargetType="ListViewItem">
  <Setter Property="Template">
      <ControlTemplate TargetType="ListBoxItem">
        <Border x:Name="Border" SnapsToDevicePixels="true">
          <GridViewRowPresenter VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
          <Trigger Property="IsSelected" Value="true">
            <Setter TargetName="Border" Property="Background" Value="Blue"/>
              <Condition Property="IsSelected" Value="false"/>
              <Condition Property="ItemsControl.AlternationIndex" Value="1"/>
            <Setter TargetName="Border" Property="Background" Value="WhiteSmoke"/>
          <Condition Property="IsSelected" Value="false"/>
          <Condition Property="ItemsControl.AlternationIndex" Value="2"/>
        <Setter TargetName="Border" Property="Background" Value="White"/>

MSDN shows example:

<Trigger Property="ItemsControl.AlternationIndex" Value="1">
  <Setter Property="Background" Value="WhiteSmoke"></Setter>
<Trigger Property="ItemsControl.AlternationIndex" Value="2">
  <Setter Property="Background" Value="White"></Setter>

Which will not work for my trigger (since I am using a Border the ControlTemplate). So instead of operating directly on ItemControl.AlternationIndex, I change the background of a different element - Border.



good old quote

I was looking through my grammars book by Daniel I.A.Cohem "Introduction to Computer Theory". Chapter 16 goes into depth on Pushdown Automata Theory with Chomsky Normal Form.

Theorem 21
If L is a context-free language generated by a Context -free Grammar that includes A-productions, then there is a different context=free grammar that has no A-productions that generates either the whole language L or else generates the language of all the words in L that are not A.

If you never took grammars and after reading this were like what the..? A few pages later in the end of the proof, the author throws this great metaphor to help you understand the rules. I read it again and couldn't help laughing ...and enjoying:

Those never born need never die. 
First statistician: 
" With all the trouble in this world, it would be better if we were never born in the first place."
Second statistician:
"Yes, but how many are so lucky? Maybe one in ten thousand."


WPF listbox.SelectedItems.Add doesn't select the items?

D'oh, I guess it's binding or ref 101, make sure you iterate through correct items list, which in this case - the bound items.

lets pretend to bind the listBox to some list of states:

listBox.ItemSource = somelistOfStates;

at some point you need to select some states programmatically that come from some other list of states (you get it from some event, for example):
foreach(var state in _statesIWantToSelect)
     listBox.SelectedItems.Add (state);

Looks right, but doesn't work (does not select the states in the listBox).
Well, the reason why is that SeletedItems list does not find the state you want it to select in its list.You have to find the corresponding listbox item first then operate on that item not on some other object that doesn't have the same reference:
foreach(var state in _statesIWantToSelect)
    foreach(var item in listBox.Items)
        if(((State)item == state)