1

私は非常に混乱しています。いくつかの表示のためにバインドしているxmlファイルがあります。私の xml の 1 つのセクションは次のようになります。

<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>

基本的に、「オプション」のコンボボックスがあり、それをうまく入力できます。オプションがない場合は空白です。オプションがない場合は無効にしたいと思います。次のように変換コードを使用して、このためのコンバーターを作成しました。

//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;

コンボボックスの私の XAML マークアップは次のとおりです。

<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>

私にとって本当に紛らわしい部分は、これが逆に機能することです。オプションのサブ項目を持つエントリは無効になり、そうでないエントリは有効になります。戻り値を切り替えるだけですべてが有効になるので、オプションのないものには触れないようです。コンバーターにブレークポイントを設定しようとしましたが、値に対して表示されるのは空の文字列だけです。

誰かがここで何が起こっているのか教えてもらえますか?

4

1 に答える 1

1

一見するHasOptionsと、すべてを削除してコンバーターを更新し、見つかったときに true を返すだけです。

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

コンバーターでHasOptionsは、ステートメント内で宣言if (s.Count != 0)されていますが、その if ステートメントの範囲外の戻り値として使用されています。これでコンバーターが修正されるかどうかは完全にはわかりませんが、試してみてください。

余談ですが、コンバーターで例外がスローされると、WPF はそれを飲み込みます。出力ウィンドウを確認すると、コンバーターの失敗によるバインディング エラーが表示される可能性があります。

于 2012-09-18T18:51:50.547 に答える