我创建了这个DataTemplate
,但我不知道如何将 astyle
与 a添加DataTrigger
到img变量中。
我希望img根据Suppliers[i].Stock
(int)的值显示不同的图像
资源图标
Properties.Resources.InStock => Suppliers[i].Stock > 0
Properties.Resources.OutOfStock => Suppliers[i].Stock = 0
Properties.Resources.Unknown => Suppliers[i].Stock = null
到目前为止我的代码。
private DataTemplate GetStockTemplate(int i)
{
var template = new DataTemplate();
var wp = new FrameworkElementFactory(typeof (WrapPanel));
wp.SetValue(FrameworkElement.HorizontalAlignmentProperty, HorizontalAlignment.Right);
wp.SetValue(WrapPanel.OrientationProperty, Orientation.Horizontal);
var tx = new FrameworkElementFactory(typeof (TextBox));
tx.SetBinding(TextBox.TextProperty, new Binding("Suppliers[" + i + "].Stock") {StringFormat = "{0:n0}"});
tx.SetValue(TextBoxBase.IsReadOnlyProperty, true);
tx.SetValue(Control.BorderThicknessProperty, new Thickness(0));
tx.SetValue(Control.BackgroundProperty, Brushes.Transparent);
tx.SetValue(TextBox.TextAlignmentProperty, TextAlignment.Right);
wp.AppendChild(tx);
var img = new FrameworkElementFactory(typeof (Image));
wp.AppendChild(img);
template.VisualTree = wp;
template.Seal();
return template;
}
我认为触发器可以工作的方式是,创建一个显示 InStock 图标的默认样式,然后在 forStock = null
和另一个 for上设置两个触发器Stock = 0
由于我是动态执行此操作的,因此我无法使用 xaml,并且其他所有内容都可以使用DataTemplate
.
解决方案
在@akjoshi 的帮助下,这就是我最终使用的。
var img = new FrameworkElementFactory(typeof(Image));
var binding = new Binding("Suppliers[" + i + "].Stock") {Converter = new StockIconConverter()};
img.SetBinding(Image.SourceProperty, binding);
wp.AppendChild(img);
class StockIconConverter :IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null || (int)value < 0)
return ConvertIconToBitmapImage(Properties.Resources.Unknown);
return ConvertIconToBitmapImage((int)value == 0 ? Properties.Resources.OutOfStock : Properties.Resources.InStock);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
#region Helper
private static BitmapImage ConvertIconToBitmapImage(Icon icon)
{
var bitmap = icon.ToBitmap();
var ms = new MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
var bImg = new BitmapImage();
bImg.BeginInit();
bImg.StreamSource = new MemoryStream(ms.ToArray());
bImg.CreateOptions = BitmapCreateOptions.None;
bImg.CacheOption = BitmapCacheOption.Default;
bImg.EndInit();
bImg.Freeze();
ms.Close();
return bImg;
}
#endregion
}