I'm trying to display an image for a row when that row has a file (in this case a PDF) attached to it. I have a True/False column called 'FileExists' which indicates if the row has a document associated with it.
So based on this this those rows that have a True value in the FileExists column should display an image. I want the ability then to allow the user to click the image to open the assigned PDF file.
I have had a few attempts at this now based on other posts but can quite get it to work properly.
Any help would be most appreciated.
Troy
Hello Troy,
Thank you for your post. I have been looking into your requirement and I created a sample project for you with the functionality you want. Basically I created a Style for the CellValuePresenter of the Boolean Field and change its ControlTemplate to has an Image and added a Converter to set different Images depending on the value of the Field. Also I bind the IsEnabled Property to the value, too, so that only the images of the records that has File can be clicked. Please let me now if this helps you or you need further assistance on this matter.
Looking forward for your reply.
Thanks Stefan! That worked like a charm, exactly what I was trying to achieve. Much appreciated.
Stefan, I have a follow up question. How can I get a value from another column for the same row wthin the Image_PreviewMouseLeftButtonDown event handler? For instance I'm trying to get an id value for that row so i can know which attached file to open. Currently the file is stored in the database and I'll have to retrieve it based on an id value.
You can add the following code in the vent handler to get the CellValuPresenter that holds the Image:
CellValuePresenter cvp = Utilities.GetAncestorFromType(sender as DependencyObject, typeof(CellValuePresenter), true) as CellValuePresenter;
And then you can cast its Record Property to DataRecord and get the Cell’s Value you need.
Hope this helps you.
That worked. Thanks.
Now I'm going to take this a step further and have the image displayed based on the extension of the file. I'm saving the file extension when I save the file to the database. so i could easily display the appropriate image based on the file extension within the MyConvert class however I have changed the FileExists column which was a boolean to a FileExtension column (varchar(10)). I would like the value of the FileExtension column to be passed to the MyConverter class however the 'value' parameter (although of type object) is expecting to be a bool and I can not convert 'value' to a string.
What am i missing here?
I have been looking into your requirement and I can say that the MyConverter class value can be cast to every Type since it is of type Object, so you can change the body of the Convert method and cast the value to String and determine which picture to return.
Hi Stefan,
I'm trying to add another piece to this now. In the below Convert method I would like to reference another column to get another piece of information which I will use in combination witht he current column to choose which icon to display. The below I've tweaked to show what I want to do.
///
<summary>
/// Converter class that converts the true value of the Direction column to an image to display in the grid control
/// informing the user the direction (sent or received) of the message.
/// </summary>
public class DirectionConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
if (value != null)
string fileExtension = value.ToString();
if (fileExtension.ToUpper() == "INBOUND")
string messageType = //... Want to refer to another cell for data here to see if message was an email or sms.
if (messageType.ToUpper() == "EMAIL")
return "../Images/EmailReceived.png";
else
return "../Images/SmsReceived.png";
if (fileExtension.ToUpper() == "OUTBOUND")
return "../Images/EmailSent.png";
return string.Empty;
}
return Binding.DoNothing;
Thank you for your feedback. I am glad that you resolved your issue and I believe that other community members may benefit from this as well.
Thanks again.
thanks Stefan that binding change worked. I also had to make a change in my MultiValueConverter class to use the ImageSourceConverter class rather than just returning a string as the image source.
new ImageSourceConverter().ConvertFromString(imagePath + "EmailReceived.png");
thanks so much.
I can suggest you change the Binding like this:
<Binding RelativeSource="{RelativeSource AncestorType={x:Type igDP:CellValuePresenter}}" Path="Record.Cells[MessageType].Value"/>
I've generated a sample project, in it is a SQL script to run to create the dependent table and inserts 2 rows of test data, also you will need to alter the connection string which I've hardcoded for the sample purpose.
You will single a single converter as well which also displays images which is working, as you'll see when you run the app. That one is fine, it the multi converter column I'm having the issue with, the field is called 'Direction'.
Hopefully, you can see what I'm missing.
Thansk,
Hello,
It seems like that your DataContext is set to CellValuePresenter, so you have to set the Binding's Path to Record.Cells["MessageType"].Value. If this doesn't help you, could you please send me an isolated sample project, where your issue is reproduced, so I can investigate it further for you.