Friday, April 29, 2011

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; (aLine != null) { aLine.icon = "idle.jpg"; }

From stackoverflow
  • 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.

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.