我有一个带有 ItemTemplate
和一些数据绑定(bind)(包括图像)的 ListBox
。
ItemsSource
在代码隐藏中设置。一切都按预期工作,直到应用程序尝试通过更新绑定(bind)到图像源的对象成员来更改图像源。我做错了什么?
这是 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>
部分代码隐藏:
在 Window_Initialized
中:
myList.ItemsSource = myLineList;
在 myList_MouseDoubleClick
中:
Line aLine = myList.SelectedItem as Line;
if (aLine != null) {
aLine.icon = "idle.jpg";
}
最佳答案
您的“Line”类是否实现了INotifyPropertyChanged ,或使用依赖属性?它必须有某种方式来通知绑定(bind)“图标”属性的值已更改。
关于c# - WPF 图像源绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/754059/