Nếu có ai đang tìm kiếm Trình chuyển đổi giá trị để ràng buộc. Đây là những gì tôi đã sử dụng
<Image Source="{Binding Converter={StaticResource ImageConverter},ConverterParameter=\{Status\}}" />
public class StatusToImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string statusValue = parameter.ToString().ToUpper();
if (!string.IsNullOrEmpty(statusValue))
{
string result = string.Empty;
switch (statusValue)
{
case "IDLE":
result = "idle.png";
break;
case "OFFLINE":
result = "offline.png";
break;
default:
result = "online.png";
break;
}
var uri = new Uri("pack://application:,,,/PIE;component/Images/" + result);
return uri;
}
return string.Empty;
}
// No need to implement converting back on a one-way binding
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
Bounded Enum
public enum DevStatus
{
Idle = 1,
Offline = 2,
Active = 3,
}
Set Enum từ ViewModel và chuyển đổi sẽ ràng buộc các hình ảnh thích hợp.
<Image Source="{Binding DevStatus, Converter={StaticResource ImageConverter}}" />
Tính năng này có hoạt động cho các thuộc tính của các thành phần khác không? Ví dụ, nếu tôi muốn kết buộc một hình ảnh dựa trên giá trị của một nhãn, điều đó có hoạt động giống nhau không? Bạn có thể xây dựng? – user3841581