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.
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;
Here you can see how to achieve the result you want:
http://mytorrey.com/2011/05/08/conditionally-formatting-cells-in-xamdatagrid-2/
Hope this helps you.
Hey Stefan,
That gets me much closer, I'm just struggling with how to 'call' the multiconverter class from within the Source property of the Image, see below for xaml. This is where my requirement differs from the link you provided.
If I try to wire in the MultiValueConverter class instead of the ValueConverter class like I'm doing for another column then I get the below error,
A 'MultiBinding' cannot be set on the 'Converter' property of type 'Binding'. A 'MultiBinding' can only be set on a Dependency Property of a DependencyObject.
<igDP:Field Name="FileExtension" Label="">
<igDP:Field.Settings>
<igDP:FieldSettings.CellValuePresenterStyle>
<Style TargetType="{x:Type igDP:CellValuePresenter}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Image ToolTip="View attachment" Cursor="Hand" Width="30" PreviewMouseLeftButtonDown="Image_PreviewMouseLeftButtonDown"
IsEnabled="{Binding RelativeSource={RelativeSource AncestorType={x:Type igDP:CellValuePresenter}},Path=Value}"
Source="{Binding RelativeSource={RelativeSource AncestorType={x:Type igDP:CellValuePresenter}},Path=Value,Converter={StaticResource converter}}">
</Image>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</igDP:FieldSettings.CellValuePresenterStyle>
</igDP:FieldSettings>
</igDP:Field.Settings>
</igDP:Field>
I've also created a style for the 2 columns I'm dealing with in the MultiValueConverter class as below, I'm just having some difficulty trying to get this all wired together.
<igDP:XamDataGrid.Resources>
<Style TargetType="{x:Type igDP:CellValuePresenter}" x:Key="TotalSalesFieldStyle">
<Setter Property="Foreground">
<MultiBinding Converter="{StaticResource directionConverter}">
<Binding Path="DataItem.Direction" />
<Binding Path="DataItem.MessageType" />
</MultiBinding>
</igDP:XamDataGrid.Resources>
Here it is shown how to use MultiBinding:
http://msdn.microsoft.com/en-us/library/system.windows.data.multibinding.aspx
I'm very close now...I'm just having a problem getting the value of the other column... keep getting {DependencyProperty.UnsetValue}
I'm thinking I'm not referencing the column name 'MessageType' properly, I've tried everyting I can think of like 'DataContext.MessageType', etc.... I' must be missing some silly thing here. The binding to 'Value' gets the current column value just fine.
<Image.Source>
<MultiBinding Converter="{StaticResource directionConverter}" >
<Binding Path="Value" />
<Binding Path="MessageType" />
</Image.Source>
I'm setting the DataContext in the Constructor of the userControl,
adapter.Connection.ConnectionString =
new Database(messageInfo.DatabaseGuid).GetDatabaseConnectionString();
adapter.Fill(dataset.tMessageTransaction, (int)messageInfo.TransactionType, messageInfo.TransactionId);
this.DataContext = dataset.tMessageTransaction.DefaultView;
Thanks in advance for any suggestions you may have.
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.