code_junkie

How can I replace an image in a WPF grid with another control depending on the source data?

Nov 13th, 2011
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. +---------+---------+
  2. | Image | Details |
  3. | is | pane |
  4. | here | for data|
  5. | | entry |
  6. +---------+---------+
  7. | ListView here to |
  8. | select data item |
  9. | for top two panes |
  10. +---------+---------+
  11.  
  12. <Grid DataContext="{Binding Source={StaticResource MyData}}">
  13. <!-- row 0 col 0 -->
  14. <Image x:Name="imgPhoto" Source="{Binding ElementName=MyListViewOfData, Path=SelectedItem.PathToImageOnDisk}" />
  15.  
  16. <!-- row 0 col 1 -->
  17. <StackPanel DataContext="{Binding ElementName=MyListViewOfData, Path=SelectedItem}">
  18. <TextBox Name="NameTextBox" Text="{Binding Name}" />
  19. <TextBlock Name="DateCreatedTextBlock" Text="{Binding DateCreated}" />
  20. </StackPanel>
  21.  
  22. <!-- row 1 cols 0,1 -->
  23. <ListView ItemsSource="{Binding}" ItemTemplate="{StaticResource MyListViewTemplate}"
  24. IsSynchronizedWithCurrentItem="True" Name="MyListViewOfData" />
  25.  
  26. </Grid>
  27.  
  28. <Grid DataContext="...">
  29. <ContentPresenter Content="{Binding SelectedItem, ElementName=MyListViewOfData}">
  30. <ContentPresenter.ContentTemplate>
  31. <DataTemplate>
  32. <Grid>
  33. <Grid.ColumnDefinitions>
  34. <ColumnDefinition />
  35. <ColumnDefinition />
  36. </Grid.ColumnDefinitions>
  37. <Image x:Name="imgPhoto" Source="{Binding PathToImageOnDisk}" />
  38. <TextBlock x:Name="error" Visibility="Collapsed" Text="Sorry, no image available" />
  39. <StackPanel Grid.Column="1">
  40. <TextBox Name="NameTextBox" Text="{Binding Name}" />
  41. <TextBlock Name="DateCreatedTextBlock" Text="{Binding DateCreated}" />
  42. </StackPanel>
  43. </Grid>
  44. <DataTemplate.Triggers>
  45. <DataTrigger Binding="{Binding PathToImageOnDisk}" Value="{x:Null}">
  46. <Setter TargetName="imgPhoto" Property="Visibility" Value="Collapsed" />
  47. <Setter TargetName="error" Property="Visibility" Value="Visible" />
  48. </DataTrigger>
  49. </DataTemplate.Triggers>
  50. </DataTemplate>
  51. </ContentPresenter.ContentTemplate>
  52. </ContentPresenter>
  53. <ListView ItemsSource="{Binding}" ItemTemplate="{StaticResource MyListViewTemplate}" IsSynchronizedWithCurrentItem="True" Name="MyListViewOfData" />
  54.  
  55. </Grid>
Add Comment
Please, Sign In to add comment