【问题标题】:WPF binding an image column in a datagrid through a converter, can't update image on property changeWPF通过转换器绑定数据网格中的图像列,无法在属性更改时更新图像
【发布时间】:2011-11-04 21:01:18
【问题描述】:

我有这个绑定到可观察项目集合的数据网格,如下所示:

<DataGrid ItemsSource="{Binding Path=MyItems}">

然后,其中一列通过一个简单的转换器绑定到 MyItems 的一个属性,该转换器将 bool 切换为图像路径。

<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Image Name="DownloadedIcon" Source="{Binding Converter={StaticResource BoolToImageCheckmark}, ConverterParameter=IsDownloaded, UpdateSourceTrigger=PropertyChanged}" Width="16" Height="16" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

属性本身 IsDownloaded 完全实现了 INotifyPropertyChanged。

这可以正常工作,因为显示的数据与集合的值匹配,并且图像列根据属性值正确显示图像。

当属性发生变化时,麻烦就来了。如果我直接在属性上绑定文本列,则更新属性时内容将更新。但是,通过转换器的图像列将不会收到更新通知。

有什么想法吗?

【问题讨论】:

  • 提供不请自来的建议,如果您只使用样式触发器,这种情况看起来确实会简单得多(即:折腾转换器)。

标签: c# wpf binding mvvm datagrid


【解决方案1】:

试试这个:

<DataTemplate>
      <Image Name="DownloadedIcon" Source="{Binding Path=IsDownloaded,Converter={StaticResource BoolToImageCheckmark}}" Width="16" Height="16" />
</DataTemplate>

还要在转换器中放置一个断点,以验证绑定是否确实有效。 请注意,您将通过转换器中的Value 参数获得绑定值。

【讨论】:

  • 输出窗口没有显示失败的绑定,还是我遗漏了某种微妙之处?
  • @RitchMelton:输出窗口确实显示运行时绑定失败,但我更喜欢在转换器中放置断点。那有两个原因: 1. 输出有时会变得非常混乱。 2.你可以通过绑定来验证你得到的值。
【解决方案2】:

传递给 ConverterParameter 的值不会响应 PropertyChanged 通知。在绑定中使用 Path 而不是 ConverterParameter,然后在转换器的 Convert() 函数中引用 value 参数而不是 parameter 参数。

【讨论】:

    【解决方案3】:

    ConverterParameter 不是依赖属性,因此您不能像尝试那样将其绑定到属性。您应该将图像源绑定到 IsDownloaded 属性并将其转换:

    <DataTemplate>
      <Image Name="DownloadedIcon" Source="{Binding Path=IsDownloaded,Converter={StaticResource BoolToImageCheckmark}}" Width="16" Height="16" />
    </DataTemplate>
    

    【讨论】:

      【解决方案4】:

      问题出在您的转换器类中。
      由于您的绑定表达式未指定“路径”,因此当前 DataContext 用作路径并导致 DataContext 对象作为转换器类中的值。正在对这个 datacontext 对象的副本执行计算。
      这种方法在执行绑定时会在第一时间成功。结果,图像列正确显示了图像。
      后来“IsDownloaded”属性发生变化,它反映在 ObservableCollectionClass 中,但是图像控件无法理解这种变化,因为它的源属性没有绑定到任何集合类属性。类似地,当转换器类收到数据上下文对象的副本时,属性更改也不会反映在转换器类中。
      因此将图像源属性设置为集合类属性“IsDownloaded”。此属性发生的任何更改都将触发具有新值的转换器类。

      图片名称="DownloadedIcon" Source="{Binding Path=IsDownloaded,Converter={StaticResource BoolToImageCheckmark}}" Width="16" Height="16"/>

      不需要UpdateSourceTrigger。

      【讨论】:

        【解决方案5】:

        实际上,您没有将图像绑定到属性 IsDownloaded,而是将其绑定到列表中的整个对象。路径很重要。

        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Image Name="DownloadedIcon" Source="{Binding Converter={StaticResource BoolToImageCheckmark}, ConverterParameter=IsDownloaded, Path=IsDownloaded, UpdateSourceTrigger=PropertyChanged}" Width="16" Height="16" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-09-10
          • 1970-01-01
          • 2013-06-17
          • 2021-04-06
          • 2013-08-16
          相关资源
          最近更新 更多