2

메서드에서 가져온 텍스트 블록에 값을 바인딩 할 수있는 방법이 있습니까? 예를 들어, Person 객체를 HierarchicalDataTemplate으로 전달합니다. 거기에서 Weight 속성에 액세스 할 수 있습니다. 이제 화성에서 무게를 얻고 싶다고 말하면서, EarthWeight int의 매개 변수를 취하는 InMars 메서드를 호출합니다. 이제 지구의 무게가 사람마다 변화 할 것입니다. 매번이 매개 변수를 어떻게 설정할 수 있습니까?WPF HierarchicalDataTemplate 내부의 매개 변수가있는 메서드에 바인딩

답변

3

가장 좋은 방법은 변환기를 사용하는 것입니다.

public class WeightOnMarsConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     // value will be the persons weight 
    } 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotSupportedException("This method should never be called"); 
    } 
} 

그런 다음 바인딩을 설정하면됩니다.

<l:WeightOnMarsConverter x:key="weightOnMars" /> <-- Add this to the resources 

{Binding Path=Weight, Converter={StaticResource weightOnMars}}