1

I have a custom made item template for my listview with which values getting bound in .xaml itself. I want to change one of the image dynamically in c# side. I know in c# I can change in ContainerContentChanging but I am not able to access the custom templates image in c# please guide me out of this problem.

Here is my listview

<ListView ItemsSource="{Binding testList}" IsItemClickEnabled="True" ContainerContentChanging="ListView_ContainerContentChanging">
  <ListView.ItemTemplate>
    <DataTemplate>
      <Grid HorizontalAlignment="Stretch">
        <Grid.RowDefinitions>
          <RowDefinition Height="1*" />
          <RowDefinition Height="2" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="0.80*"></ColumnDefinition>
          <ColumnDefinition Width="0.20*"></ColumnDefinition>
        </Grid.ColumnDefinitions>

        <StackPanel Margin="0,8,0,8" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center">
          <StackPanel Orientation="Horizontal">
            <TextBlock FontSize="20" Margin="0,8,0,4" Text="{Binding textVal1}" />
            <Image Name="imgStatus" Source="ms-appx:///Assets/Icons/ic_test.png" Height="36" />
          </StackPanel>
          <TextBlock FontSize="18" Margin="0,0,0,4" Text="{Binding textVal2}" />
          <TextBlock FontSize="21" Margin="0,0,0,0" Text="{Binding textVal3}" TextWrapping="WrapWholeWords" MaxLines="2" />
        </StackPanel>
      </Grid>
    </DataTemplate>
  </ListView.ItemTemplate>
</ListView>

I thought lets have the data template in a separate c# file ... but I am not able to link up the c# file inside the grid.

Any help to access the image from the data template is appreciated.

Mahdi
  • 3,199
  • 2
  • 25
  • 35
Shiva
  • 545
  • 1
  • 10
  • 41

1 Answers1

0

If you define the templates as a static resource, or elsewhere where they can be referenced in XAML, you can use a custom converter with appropriate bindings to handle the change in template.

However, I believe changing the ListView.ItemTemplate will affect all items in the collection. In order to change the template of individual items, you will have to add a wrapper object and modify that object's content (which you can do in a similar manner).

I understand you wished to change the template in C#, but I believe XAML's declarative style should be sufficient.