【问题标题】:Refreshing a binding that uses a value converter刷新使用值转换器的绑定
【发布时间】:2010-03-18 10:54:36
【问题描述】:

我有一个绑定到对象的 WPF UI。我正在使用 ValueConverter 通过业务规则将属性转换为特定图像:

public class ProposalStateImageConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { var proposal = value as Proposal; var basePath = "pack://application:,,,/ePub.Content;component/Images/General/Flag_{0}.png"; string imagePath; if(proposal.Invoice != null) { imagePath = string.Format(basePath, "Good"); } else { imagePath = string.Format(basePath, "Warning"); } var uri = new Uri(imagePath); var src = uri.GetImageSource(); //Extention method return src; } }

该元素是一个TreeView,其中图像位于第二级:

<TreeView x:Name="tree" ItemsSource="{Binding People}" SelectedItemChanged="OnTreeItemChanged"> <TreeView.Resources> <HierarchicalDataTemplate DataType="{x:Type dmn:Person}" ItemsSource="{Binding Proposals}"> <StackPanel Orientation="Horizontal" ToolTip="{Binding Path=Fullname}" Margin="3"> <Image Margin="5,0,5,0" Width="16" Height="16" Source="pack://application:,,,/ePub.Content;component/Images/General/Person_Active.png" /> <TextBlock Text="{Binding Path=Firstname}" /> <TextBlock Text="{Binding Path=Lastname}" Margin="5,0,0,0" /> </StackPanel> </HierarchicalDataTemplate> <HierarchicalDataTemplate DataType="{x:Type dmn:Proposal}"> <StackPanel Orientation="Horizontal" Margin="3"> <Image x:Name="invoiceImage" Width="16" Height="16" Margin="5,0,5,0" Source="{Binding, Converter={StaticResource ProposalStateImageConverter}, UpdateSourceTrigger=PropertyChanged}" /> <TextBlock Text="{Binding DeliveryDate, Converter={StaticResource textCulturedDateConverter}}" /> </StackPanel> </HierarchicalDataTemplate> </TreeView.Resources> </TreeView>

它工作正常,但后来,当对象的状态发生变化时,我想刷新图像并使值转换器重新评估。这怎么可能?

【问题讨论】:

    标签: wpf binding ivalueconverter


    【解决方案1】:

    看起来您只在转换器中使用了一个值,并且只是在两个值之间进行了简单的切换,因此您可以直接在 XAML 中使用触发器来执行此操作。此方法还切换到针对 Invoice 属性的绑定,以便该属性的任何更改通知都会导致触发器更新。

    <HierarchicalDataTemplate >
        <StackPanel Orientation="Horizontal" Margin="3">
            <Image x:Name="invoiceImage" Width="16" Height="16" Margin="5,0,5,0" Source="good.png"/>
            <TextBlock ... />
        </StackPanel>
        <HierarchicalDataTemplate.Triggers>
            <DataTrigger Binding="{Binding Invoice}" Value="{x:Null}">
                <Setter TargetName="invoiceImage" Property="Source" Value="warning.png"/>
            </DataTrigger>
        </HierarchicalDataTemplate.Triggers>
    </HierarchicalDataTemplate>
    

    【讨论】:

      【解决方案2】:

      假设由于绑定到整个对象而无法使用 INotifyPropertyChanged,则需要调用 BindingExpression.UpdateTarget

      细微之处在于掌握绑定表达式。这需要您对视图有相当深入的了解:据我所知,唯一的方法是调用BindingOperations.GetBindingExpression,传递您要更新其绑定的控件和属性,例如:

      BindingOperations.GetBindingExpression(myImage, Image.SourceProperty).UpdateTarget();
      

      【讨论】:

      • 即使在 UI 上做起来还是有点复杂。绑定的元素是 TreeView,我想刷新第二级的图像。我已经添加了上面的 UI 代码。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-27
      • 2016-12-18
      相关资源
      最近更新 更多