Hello,
I'm looking forword to find a way to display an image into a boolean field. I found some posts to display an image when the field contains the path but not with a boolean value.
So as the value is "True" I want to displai Image1.png and as the value is "False" I want to display Image2.png
Thanks
It seems like there are a ton of related questions today!
I replied to this same question elsewhere. You can also create a style for that boolean field and use triggers in the style to control the visibility of the icon. This is the inner trigger code, but I posted the full example earlier.
<DataTrigger Binding="{Binding Path=DataItem, Converter={StaticResource maintenanceConverter}}"
Value="True">
<Setter Property="Visibility"
TargetName="Image"
Value="Visible" />
</DataTrigger>
Hi -
I think your solution is a good one, however I would not use the code you have in InitializeRecord. Instead I would specify a custom converter in the Binding you have for the Source property of the Image element in the 'FlagField' custom CellValuePresenter style:
<Image Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content, Converter={StaticResource myBoolToImageConverter}}"/>
The converter resource would be defined in your project's resources:
<local:MyBoolToImageConverter x:Key="myBoolToImageConverter"/>
The converter would look at the boolean value being passed in and convert it to the proper image:
public class MyBoolToImageConverter : IValueConverter {
public class MyBoolToImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value is bool) { bool boolValue = (bool)value; Image img; if (boolValue == true) img = (Image)App.Current.FindResource("GreenFlag"); else img = (Image)App.Current.FindResource("RedFlag"); return img.Source; } return null; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); }
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
if (value is bool) { bool boolValue = (bool)value; Image img; if (boolValue == true) img = (Image)App.Current.FindResource("GreenFlag"); else img = (Image)App.Current.FindResource("RedFlag"); return img.Source; } return null;
if (value is bool)
bool boolValue = (bool)value; Image img; if (boolValue == true) img = (Image)App.Current.FindResource("GreenFlag"); else img = (Image)App.Current.FindResource("RedFlag"); return img.Source;
bool boolValue = (bool)value;
Image img;
if (boolValue == true)
img = (Image)App.Current.FindResource("GreenFlag");
else
img = (Image)App.Current.FindResource("RedFlag");
return img.Source;
}
return null;
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
throw new NotImplementedException();
Joe
I found a solution but I don't think this way is the best...
This solution works for me because I'm using a BindingList of known object, it's possible to insert some data in the grids that are not coming from object...
Here is it :
First of all, you have to put your image into the Ressouces of you project, in Application.Resources like this
<Image Source="..." />
As I found in the stand alone help (in Displaying on image into a Field) we have to create a special CellValuePresenter, I took the one I found in the help :
<Style TargetType="{x:Type igDP:CellValuePresenter}" x:Key="FlagField"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}"> <Grid Height="20"> <Image Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"/> </Grid> </ControlTemplate> </Setter.Value> </Setter> </Style>
After that, create an UnboundField into the grid like this
<igDP:UnboundField Name="ImageField" Label="Image"> <igDP:Field.Settings> <igDP:FieldSettings CellMinWidth="100" CellMaxWidth="300" CellValuePresenterStyle="{StaticResource FlagField}" /> </igDP:Field.Settings> </igDP:UnboundField>
Add an event handler for InitializeRecord in you grid like this :
InitializeRecord="MyXamDataGrid_InitializeRecord"
So in the handler :
DataRecord dr = e.Record as DataRecord; if (dr != null) { MyObject obj = dr.DataItem as MyObject; Cell cell = dr.Cells["ImageField"]; Image img; if (obj .TheBooleanField == true) img = (Image)App.Current.FindResource("GreenFlag"); else img = (Image)App.Current.FindResource("RedFlag"); cell.Value = img.Source; }
The trick is done !
As i say, there is perhaps a best way to proceed but I haven't found it.... So... This solution works pretty well !
I hope that the Infragistics team will comment this solution and give a best solution (if there is...)
gle