Click here to Skip to main content
15,890,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I have this datatemplate in my datagrid

XML
<DataGridTemplateColumn  Header="Ferdig" Width="*">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate >
                                <Grid Height="auto" Background="#FF3F3F46">
                                    <Grid Height="Auto">
                                        <Grid.RowDefinitions>
                                            <RowDefinition Height="Auto"/>
                                            <RowDefinition Height="Auto"/>
                                        </Grid.RowDefinitions>
                                        <TextBox  Text="{Binding Tegn}" IsReadOnly="True" Grid.Row="0" Margin="5,5,0,5" FontSize="30"/>
                                        <TextBox  Text="{Binding Varenr}" IsReadOnly="True" Grid.Row="0" Margin="0,5,5,5" FontSize="18" HorizontalAlignment="Right"/>
                                        <Grid Height="Auto" Grid.Row="1">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="*"/>
                                            </Grid.ColumnDefinitions>
                                            <TextBlock Text="Utfordring" Grid.Column="0" FontSize="12"  Margin="5,5,5,5"/>
                                            <Image Source="P:\Avd_Storkunde\02 Dosepakking (ML)\20 IT (JSH)\209 Tablettbilder\2091 Dagens bilder\5.jpg" Grid.Column="0"  Margin="5,25,5,5" HorizontalAlignment="Stretch" VerticalAlignment="Top"/>
                                            <TextBlock Text="Forslag til tiltak" Grid.Column="1" FontSize="12"  Margin="5,5,5,5"/>
                                            <TextBox Text="{Binding Forslag_til_tiltak}" IsReadOnly="True" Grid.Column="1"  Margin="5,25,5,5" VerticalAlignment="Top" FontSize="16" TextWrapping="Wrap"/>
                                        </Grid>
                                    </Grid>
                                </Grid>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>



How can i make the imagesource dynamic and bound?

I want to do something like this:

XML
<Image Source="P:\Avd_Storkunde\02 Dosepakking (ML)\20 IT (JSH)\209 Tablettbilder\2091 Dagens bilder\" + {Binding Varenr} + ".jpg" Grid.Column="0"  Margin="5,25,5,5" HorizontalAlignment="Stretch" VerticalAlignment="Top"/>



How can i arrive at my goal?
Posted

1 solution

there are several ways to achieve this:

1.) provide a property in your databound object that holds an Uri you can easyly bind to

2.) use the Binding's StringFormat-property[^]

3.) use a custom value converter[^]:

C#
namespace WpfApplication1 {
  [ValueConversion(typeof(object), typeof(String))]
  public class StringFormatConverter : IValueConverter {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
          var formatString = string.IsNullOrEmpty(FormatString)? "{0}" : FormatString;
          return string.Format(culture, FormatString, value);
      }

      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
          throw new NotImplementedException();
      }

      public string FormatString{ get; set; }
  }
}

}


define an instance inside your xaml:
XML
<Window xmlns:local="clr-namespace:WpfApplication1">
  <Window.Ressources>
     <local:StringFormatConverter x:key="imageSourceConverter" FormatString="p:\Folder\subfolder\{0}.jpg" /> 
  </Window.Ressources>

<!--use the converter -->
<Image Source="{Binding Varenr, Converter={StaticResource imageSourceConverter}}" />
 
Share this answer
 
v6

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900