2011-03-17 5 views
0

일부 int 값이 변경되면 소스가 변경되는 이미지를 표시하는 응용 프로그램이 있습니다.이미지 소스에 이미지 소스 바인딩하기

<Image Source="{Binding Path=Gas, Converter={StaticResource GasToImageSource}}"/> 

(가스 int 값입니다) : , 나는 이미지의 '소스'속성을 결합하기 위해 노력하고있어 그렇게합니다. 그리고 변환기 :

public class GasToImageSource : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     int gas_value = (int)value; 
     if (gas_value <=500) 
      return SomeNameSpace.Properties.Resources.GAS_INDICATOR1; 
     if (gas_value <=1000) 
      return SomeNameSpace.Properties.Resources.GAS_INDICATOR2; 
     if (gas_value <= 1500) 
      return SomeNameSpace.Properties.Resources.GAS_INDICATOR3; 

     return SomeNameSpace.Properties.Resources.GAS_INDICATOR4; 
    } 

    ... 
} 

그러나 이것은 어떤 이유로 작동하지 않습니다. 제 바인딩에 어떤 문제가 있습니까?

답변

0

문자열이 아닌 ImageSource 개체를 제공해야합니다. 그런 식으로 뭔가를 시도 :

public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
{ 
    int gas_value = (int)value; 
    string res = SomeNameSpace.Properties.Resources.GAS_INDICATOR4; 
    if (gas_value <=500) 
     res = SomeNameSpace.Properties.Resources.GAS_INDICATOR1; 
    if (gas_value <=1000) 
     res = SomeNameSpace.Properties.Resources.GAS_INDICATOR2; 
    if (gas_value <= 1500) 
     res = SomeNameSpace.Properties.Resources.GAS_INDICATOR3; 

    BitmapImage img = new BitmapImage(); 
    img.BeginInit(); 
    img.UriSource = new Uri(res, UriKind.Relative); 
    img.EndInit(); 
    return img; 
} 
1

문자열을 사용하려면이이 결합을 변경할 수 있습니다

Source="{Binding Path=Gas, StringFormat={}/your_namespace;component/{0}, Converter={StaticResource GasToImageSource}}"