2012-09-18 3 views
1

나는 매우 혼란 스럽다. 일부 디스플레이에 바인딩 할 XML 파일이 있습니다. 내 XML의 한 부분은 다음과 같습니다 XML 바인딩이 반대로되어 있습니까?

<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 [email protected]}" /> 
    </DataTemplate> 
    </ComboBox.ItemTemplate> 
</ComboBox> 

진짜로 혼란스러운 부분이 역으로 작동합니다. 옵션 하위 항목이있는 항목은 사용할 수없는 항목은 사용할 수 없게 설정됩니다. 반환 값을 전환하면 모든 옵션이 활성화되므로 옵션이없는 옵션은 절대 건드리지 않는 것과 같습니다. 변환기에 중단 점을 넣으려고했지만 모든 값을 표시하는 빈 문자열입니다.

누군가 여기서 무슨 일이 일어 났는지 말해 줄 수 있습니까?

답변

1

언뜻 보면, 모두 함께 HasOptions을 삭제하여 변환기를 업데이트하고 발견했을 때 그럼 그냥 true를 반환 :

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

, HasOptionsif (s.Count != 0) 문 내부에 선언되어 있지만, 반환로 사용되는 그 if 문 범위 밖의 값. 나는 이것이 변환기를 바로 잡을 것이라고는 확신 할 수는 없지만 한번 시도해 보라.

예외 : 변환기에서 예외가 발생하면 WPF가이를 삼킨다. 출력 창을 확인하면 변환기 오류로 인해 바인딩 오류가 발생할 수 있습니다.

+0

포인터 주셔서 감사합니다. 나는 그것을 깨끗하게했고 불행히도 도움이되지 못했습니다. – sfaust

+0

나는 그것을 알아 냈다. (또는 어쨌든 그것을 가지고있다.) xml 바인딩을 사용하는 대신 'XPath =./*'를 'Path = ChildNodes'로 변경하여 표준 바인딩을 사용했습니다. 감사! – sfaust

+0

또한 팁을 +1 하겠지만 아직 충분한 평판이 없습니다. – sfaust

관련 문제