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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:52:51  来源:igfitidea点击:

WPF Image Source Binding

c#wpfimagexamlbinding

提问by

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

我有一个ListBoxwith ItemTemplates 和一些数据绑定,包括一个 Image。

The ItemsSourceis 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,或使用依赖属性?它必须通过某种方式通知绑定“图标”属性的值已更改。