Martin Roth
Martin Roth

Reputation:

WPF Image Source Binding

I have a ListBox with ItemTemplates and some data bindings, including an Image.

The ItemsSource is set in the codebehind. Everything works as expected until the app tries to change the image's source by updating the object's member, which is bound to the image source. What am I doing wrong?

Here's the XAML:

<ListBox x:Name="myList" MouseDoubleClick="myList_MouseDoubleClick" ScrollViewer.VerticalScrollBarVisibility="Disabled">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Vertical" />
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Border BorderBrush="DarkGray" BorderThickness="1">
                <StackPanel Orientation="Horizontal" Width="100">
                    <Image Width="38" Height="38" Source="{Binding Path=icon}" />
                    <StackPanel Width="100">
                        <Label Content="{Binding Path=name}" />
                        <Label Content="{Binding Path=state}" />
                    </StackPanel>
                </StackPanel>
            </Border>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Some parts of the codebehind:

In Window_Initialized:

myList.ItemsSource = myLineList;

In myList_MouseDoubleClick:

Line aLine = myList.SelectedItem as Line;
if (aLine != null) {
  aLine.icon = "idle.jpg";
}

Upvotes: 2

Views: 11559

Answers (1)

Matt Hamilton
Matt Hamilton

Reputation: 204129

Does your "Line" class implement INotifyPropertyChanged, or use dependency properties? It has to have some way to notify the binding that the "icon" property's value has changed.

Upvotes: 1

Related Questions