C# WPF 图像源绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/754059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Image Source Binding
提问by
I have a ListBox
with ItemTemplate
s and some data bindings, including an Image.
我有一个ListBox
with ItemTemplate
s 和一些数据绑定,包括一个 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?
将ItemsSource
在代码隐藏设置。一切都按预期工作,直到应用程序尝试通过更新绑定到图像源的对象成员来更改图像的源。我究竟做错了什么?
Here's the XAML:
这是 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
:
在Window_Initialized
:
myList.ItemsSource = myLineList;
In myList_MouseDoubleClick
:
在myList_MouseDoubleClick
:
Line aLine = myList.SelectedItem as Line;
if (aLine != null) {
aLine.icon = "idle.jpg";
}
采纳答案by Matt Hamilton
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.
您的“Line”类是否实现INotifyPropertyChanged,或使用依赖属性?它必须通过某种方式通知绑定“图标”属性的值已更改。