sfaust
sfaust

Reputation: 2373

XML Binding Reversed?

I am very confused. I have an xml file that I am binding to for some display. One section of my xml looks like this:

<Section Name="Water Efficiency">
  <Prerequisite Title="Prerequisite 1" Description="Water Use Reduction—20% Reduction " />
  <Credit CanCheckFromModel="False" CheckFromModel="False" Title="Credit 1" Description="Water Efficient Landscaping" IsGoal="Yes" PossiblePoints="2 to 4">
    <Option Description="Reduce by 50%" PossiblePoints="2" />
    <Option Description="No Potable Water Use or Irrigation" PossiblePoints="4" />
  </Credit>
  <Credit CanCheckFromModel="False" CheckFromModel="False" Title="Credit 2" Description="Innovative Wastewater Technologies" IsGoal="Yes" PossiblePoints="2" />
  <Credit CanCheckFromModel="True" CheckFromModel="True" Title="Credit 3" Description="Water Use Reduction " IsGoal="Yes" PossiblePoints="2 to 4">
    <Option Description="Reduce by 30%" PossiblePoints="2" />
    <Option Description="Reduce by 35%" PossiblePoints="3" />
    <Option Description="Reduce by 40%" PossiblePoints="4" />
  </Credit>
</Section>

Basically I have a combobox for the 'options' that I can get to populate just fine and it's blank if there are no options. Now I want it to disable if there are no options. I have created a converter for this with convert code as follows:

//convert to an xmlnodelist
XmlNodeList s = value as XmlNodeList;

System.Diagnostics.Debugger.Break();

//make sure the conversion worked
if (s != null)
{
  //see if there are any nodes in the list
  if (s.Count != 0)
  {
    //has nodes, check to see if any of them are of type 'Option'
    bool HasOptions = false;
    foreach (XmlNode n in s)
    {
      if (n.Name == "Option")
      {
        //found one with an option, exit loop
        HasOptions = true;
        break;
      }
    }

    //check if we found any options and return accordingly
    return HasOptions;
  }
}

//conversion failed or count was 0, false by default
return false;

my XAML markup for the combobox is:

<ComboBox Width="200" DataContext="{Binding}"  ItemsSource="{Binding XPath=./*}" IsEnabled="{Binding XPath=./*, Converter={StaticResource ConvOptions}}" >
  <ComboBox.ItemTemplate>
    <DataTemplate>
      <TextBlock Text="{Binding XPath=@Description}" />
    </DataTemplate>
  </ComboBox.ItemTemplate>
</ComboBox>

The really confusing part to me is that this works in reverse. The entries that have option sub-items are disabled while the ones that don't are enabled. If I just switch the return values then I get everything enabled so it's like it never touches the ones that don't have options. I tried putting a breakpoint in the converter but all it ever shows me for a value is an empty string.

Can someone tell me what is going on here?

Upvotes: 1

Views: 59

Answers (1)

Metro Smurf
Metro Smurf

Reputation: 38385

At first glance, update your converter by deleting HasOptions all together and then just return true when found:

if (n.Name == "Option")
{
    //found one with an option, exit loop
    return true;
}

In the converter, HasOptions is declared inside the if (s.Count != 0) statement, but is being used as a return value outside the scope of that if statement. I'm not entirely sure this will correct the converter, but give it a shot.

On a side note: when an exception is thrown in a converter, WPF just swallows it; check your output window and you'll likely see a binding error due to the converter failing.

Upvotes: 1

Related Questions