顧客データ用のデータグリッドがあります。私の顧客エンティティには、公開されているメモのコレクションがあります。
メモのステータスに基づいて列に画像を表示する方法が必要です。メモのステータスが「警告」の場合は警告画像を表示し、そうでない場合は通常のステータスの画像を表示します。
これは実行可能ですか?
顧客データ用のデータグリッドがあります。私の顧客エンティティには、公開されているメモのコレクションがあります。
メモのステータスに基づいて列に画像を表示する方法が必要です。メモのステータスが「警告」の場合は警告画像を表示し、そうでない場合は通常のステータスの画像を表示します。
これは実行可能ですか?
はい、これを達成する方法は複数あります。
顧客がいる場合はViewModel
、特定の顧客がメモコレクションに警告ステータスを持っているかどうかを示すプロパティを公開し、それを使用して画像を表示するかどうかを決定します。
もう1つのオプションはValueConverter
、メモコレクションを取り込んで、画像を表示するかどうかを決定するを使用することです。
他のアプローチもあると思いますが、これらは私の頭に浮かんだものです。
読み取り専用の[NotMapped]プロパティをCustomerエンティティ(Entity Framework 4を使用)に追加しました。これはブール値を返し、DataGridTemplateColumn内の画像をこれにバインドし、値コンバーターを使用してソースを設定します。
実在物
[NotMapped]
public bool ShowWarning
{
get
{
if (this.AuditableNotes != null && this.AuditableNotes.Count(an => an.Warning) > 0)
{
return true;
}
else
{
return false;
}
}
}
XAML
<DataGridTemplateColumn
Header="Status">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image x:Name="MyImage" Source="{Binding ShowWarning, Converter={StaticResource notesStatusConverter}}" Width="25" Height="20"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
ValueConverter
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && (bool)value == true)
{
return "/Assets/Images/symbol_error.png";
}
else
{
return "/Assets/Images/symbol_information.png";
}
}