刷新使用值转换器的绑定 [英] Refreshing a binding that uses a value converter

查看:92
本文介绍了刷新使用值转换器的绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a WPF UI that is bound to an object. I'm using a ValueConverter to convert a property to a specific image by a business rule:



    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,图像位于第二层:

The element is a TreeView where the image is on the 2nd level:



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

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

It is working fine, but later, when the object's state changes, I want to refresh the image and make the value converter reevaluate. How is this possible?

推荐答案

看起来您只在转换器中使用了一个值,而只是在两个值之间进行了简单的切换,因此您可以这样做直接在XAML中使用触发器.此方法还将切换到针对发票"属性的绑定",以便对该属性的任何更改通知都将导致触发器更新.

It looks like you're only using a single value inside the converter and you're just doing a simple switch between two values so you could instead just do this directly in XAML with a trigger. This method also switches to a Binding against the Invoice property so that any change notifications for that property will cause the Trigger to update.

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

这篇关于刷新使用值转换器的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆