2013-10-09 1 views

답변

3

예와는 완전한 MSDN의 예이다 :이 예에서

, TemperatureScale 두 개의 매개 변수 (이중의 하나는 열거 형 TempType 중 하나)과 걸리는 방법 ConvertTemp을 가지는 클래스는 주어진 값을 한 온도 범위에서 다른 온도 범위로 변환합니다. 다음 예제에서는 ObjectDataProvider를 사용하여 TemperatureScale 객체를 인스턴스화합니다. ConvertTemp 메서드는 두 개의 지정된 매개 변수를 사용하여 호출됩니다. XAML은

<Window.Resources> 
    <ObjectDataProvider ObjectType="{x:Type local:TemperatureScale}" 
        MethodName="ConvertTemp" x:Key="convertTemp"> 
<ObjectDataProvider.MethodParameters> 
    <system:Double>0</system:Double> 
    <local:TempType>Celsius</local:TempType> 
    </ObjectDataProvider.MethodParameters> 
    </ObjectDataProvider> 

<local:DoubleToString x:Key="doubleToString" /> 

</Window.Resources> 

이제 방법은 자원으로 사용할 수 있는지, 당신은 그 결과에 바인딩 할 수 있습니다. 다음 예제에서는 TextBox의 Text 속성과 ComboBox의 SelectedValue가 메서드의 두 매개 변수에 바인딩됩니다. 이를 통해 사용자는 변환 할 온도와 변환 할 온도 범위를 지정할 수 있습니다. BindsDirectlyToSource는 ObjectDataProvider 인스턴스의 MethodParameters 속성에 바인딩하고 ObjectDataProvider (TemperatureScale 객체)로 래핑 된 객체의 속성이 아니기 때문에 true로 설정됩니다. 마지막 레이블의 내용은 사용자가 TextBox의 내용이나 ComboBox를 수정할 때 업데이트됩니다. XAML

<Label Grid.Row="1" HorizontalAlignment="Right">Enter the degree to convert:</Label> 
<TextBox Grid.Row="1" Grid.Column="1" Name="tb"> 
<TextBox.Text> 
    <Binding Source="{StaticResource convertTemp}" Path="MethodParameters[0]" 
     BindsDirectlyToSource="true" UpdateSourceTrigger="PropertyChanged" 
     Converter="{StaticResource doubleToString}"> 
    <Binding.ValidationRules> 
    <local:InvalidCharacterRule/> 
    </Binding.ValidationRules> 
</Binding> 
</TextBox.Text> 
</TextBox> 
<ComboBox Grid.Row="1" Grid.Column="2" 
    SelectedValue="{Binding Source={StaticResource convertTemp}, 
Path=MethodParameters[1], BindsDirectlyToSource=true}"> 
<local:TempType>Celsius</local:TempType> 
<local:TempType>Fahrenheit</local:TempType> 
</ComboBox> 
<Label Grid.Row="2" HorizontalAlignment="Right">Result:</Label> 
<Label Content="{Binding Source={StaticResource convertTemp}}" 
Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2"/> 

컨버터 DoubleToString 더블을 취하고 ConvertBack에 이중으로 문자열 (Text 속성 바인딩 대상에 바인딩 소스로부터) 변환 방향 캐릭터로 바뀌고 변환 방향. InvalidationCharacterRule은 유효하지 않은 문자를 검사하는 ValidationRule입니다. TextBox 주위의 빨간색 테두리 인 기본 오류 템플릿은 입력 값이 이중 값이 아닐 때 사용자에게 알리는 것으로 나타납니다.

관련 문제