2011-06-14 2 views
0
<DataTemplate> 
    <StackPanel Orientation="Vertical" Name="AddressStackPanel" > 
    <ComboBox Name="ComboBox" ItemsSource="{Binding Path=MatchedAddressList}" DisplayMemberPath="Address" SelectedIndex="0" SelectionChanged="ComboBox_SelectionChanged"/> 
    <TextBlock Name="InputtedAddress" Text="{Binding Path=InputtedAddress}" Foreground={Hopefully pass the UI element to the dataconverter } /> 
    </StackPanel> 
</DataTemplate> 

ComboBox에는 가장 높은 점수 값이 선택된 지오 데이터베이스와 일치하는 주소가 있습니다. Textblock에는 일치하는 데 사용 된 사용자 입력 주소가 있습니다. 주소가 같으면 전경을 녹색으로, 그렇지 않으면 빨간색으로합니다.전체 UI 요소를 IValueConverter에 전달할 수 있습니까?

나는 전체 TextBlock을 dataconverter로 전달하고, 부모 StackPanel을 가져오고, 자식 0을 얻고, Combobox로 캐스팅하여 0 번째 요소를 얻은 다음 비교하여 빨간색 또는 녹색을 반환 할 수 있다고 생각했습니다. 이것이 가능합니까?

그렇지 않으면 난 그냥 못생긴 내가

답변

2
<DataTemplate> 
    <StackPanel Orientation="Vertical" Name="AddressStackPanel" > 
     <ComboBox Name="ComboBox" 
        ItemsSource="{Binding Path=MatchedAddressList}" 
        DisplayMemberPath="Address" SelectedIndex="0" 
        SelectionChanged="ComboBox_SelectionChanged"/> 
     <TextBlock Name="InputtedAddress" Text="{Binding Path=InputtedAddress}" 
        Foreground={"Binding RelativeSource={x:Static RelativeSource.Self}, 
           Converter={x:StaticResource myConverter}}" /> 
    </StackPanel> 
</DataTemplate> 

예를 생각하는 I는 시각적 트리를 통과해야한다 생각합니다. 당신은 InputtedAddress에 평등을 위해 그 값을 비교하고 상응 Brushes.Green 또는 Brushes.Red를 반환하는 컨버터를 사용하여 ComboBoxSelectedItem에 결합 할 수 msdn article

1

참조하십시오.

까다로운 부분은 위에서 언급 한 변환기가 어떻게 든 InputtedAdress을 추적해야한다는 것입니다. 바인드하려면 ConverterParameter을 사용할 수 없기 때문에 이것은 상당히 번거로 롭습니다. 그래서 우리는 다소 복잡한 변환기가 필요할 것입니다.

반면에 효과는 IMultiValueConverter으로보다 쉽게 ​​구현할 수 있습니다. 예를 들어 :

<ComboBox Name="ComboBox" ItemsSource="{Binding Path=MatchedAddressList}" DisplayMemberPath="Address" SelectedIndex="0" SelectionChanged="ComboBox_SelectionChanged"/> 
<TextBlock Name="InputtedAddress" Text="{Binding Path=InputtedAddress}"> 
    <TextBlock.Foreground> 
     <MultiBinding Converter="{StaticResource equalityToBrushConverter}"> 
      <Binding ElementName="ComboBox" Path="SelectedItem" /> 
      <Binding Path="InputtedAddress" /> 
     </MultiBinding> 
    </TextBlock.Foreground> 
</TextBlock> 

그런 다음 Brush에 두 개의 입력 값을 변환하는 IMultiValueConverter 필요합니다. 이것은 documentation 제공 예제를 사용하여 정말 쉽습니다.

관련 문제