刷新使用值转换器的绑定

时间:2010-03-18 10:54:36

标签: wpf binding ivalueconverter

我有一个绑定到对象的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>

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

2 个答案:

答案 0 :(得分:3)

假设您无法使用INotifyPropertyChanged,因为您绑定了整个对象,则需要调用BindingExpression.UpdateTarget

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

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

答案 1 :(得分:3)

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

<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>