c# - WPF 图像源绑定(bind)

标签 c# wpf image xaml binding

我有一个带有 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/

相关文章:

c# - 将 WPF 应用程序作为 Windows 服务运行

html - 单击缩略图之前如何在 div 中加载图像?

image - Vowpal Wabbit 模型在使用像素 RGB 值对图像进行多类分类时表现不佳

c# - 在不同的 .NET 框架之间共享记录器

c# - 在 WPF 中使用 ItemsControl 时正确对齐控件

c# - WPF - 任务栏通知球

c# - 使用open xml和C#将图像插入到word文档中

c# - .NET Core 3.1 ChangePasswordAsync 内部异常 "Cannot update Identity column"

c# - 使用 npgsql 创建名称区分大小写的 PostgreSQL 数据库

c# - Windows Phone 8.1 RT 在 C# 中更改图像源