[英]WPF Change the PackIcon of datagrid row depending on binding value
I am trying to accomplish this: Add a Image in the DataGridTemplateColumn but instead of Image use materialDesign:PackIcon.我正在尝试完成此操作: 在 DataGridTemplateColumn 中添加一个图像,但使用 materialDesign:PackIcon 代替图像。 Here is my code.
这是我的代码。
<DataGridTemplateColumn Header="Message">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<materialDesign:PackIcon Kind="{Binding MsgStatus,Converter={StaticResource NumberToIconConverter}}" Foreground="Green"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
MsgStatus is the column value, depending on its value the icon will be different. MsgStatus 是列值,根据其值图标会有所不同。 This is accomplished through a converter which returns IconKind.
这是通过返回 IconKind 的转换器完成的。
public class NumberToIconConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
string input = value as string;
switch (input)
{
case "1":
return MaterialDesignThemes.Wpf.PackIconKind.Alarm;
case "2":
return MaterialDesignThemes.Wpf.PackIconKind.Message;
default:
return DependencyProperty.UnsetValue;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
Converter is referenced in the usercontrol but it seems that is never called and there is no errors during debugging or execution.在用户控件中引用了转换器,但似乎从未调用过,并且在调试或执行期间没有错误。
<UserControl.Resources>
<local:NameToBrushConverter x:Key="NumberToIconConverter"/>
</UserControl.Resources>
Using the same binding with other converters on the same column with Image or textblock (eg) works but with materialDesign:PackIcon the converter is never called and thus, icons don't change.使用与 Image 或 textblock 同一列上的其他转换器相同的绑定(例如)有效,但使用 materialDesign:PackIcon 转换器永远不会被调用,因此图标不会改变。 Am I wrong with the binding?
我绑定错了吗?
EDIT:编辑:
Solved: Converter was not being called, replaced reference with:已解决:未调用转换器,将参考替换为:
<UserControl.Resources>
<local:NumberToIconConverter x:Key="NumberToIconConverter"/>
</UserControl.Resources>
Converter using answer, added DBNull check to avoid Null Exception:转换器使用答案,添加 DBNull 检查以避免 Null 异常:
public class NumberToIconConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == DBNull.Value)
{
return DependencyProperty.UnsetValue;
}
var input = System.Convert.ToInt16(value);
switch (input)
{
case 1:
return MaterialDesignThemes.Wpf.PackIconKind.Alarm;
case 2:
return MaterialDesignThemes.Wpf.PackIconKind.Message;
default:
return DependencyProperty.UnsetValue;
}
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotSupportedException();
}
}
I think the problem is in your converter which you cast int
to string
.我认为问题出在您将
int
转换为string
的转换器中。 I create a sample project with your codes and this model:我用你的代码和这个模型创建了一个示例项目:
public class Messages
{
public int MsgStatus { get; set; }
}
and i changed your convert to this (look at line 2):我把你的转换改成了这个(看第2行):
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var input = System.Convert.ToInt16(value);
switch (input)
{
case 1:
return MaterialDesignThemes.Wpf.PackIconKind.Alarm;
case 2:
return MaterialDesignThemes.Wpf.PackIconKind.Message;
default:
return DependencyProperty.UnsetValue;
}
}
and it's work for me.这对我有用。 Output:
输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.