2010-03-18 41 views
3

我有一個綁定到對象的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> 
 

據工作很好,但稍後,當對象狀態改變時,我想刷新圖像並重新評估值轉換器。這怎麼可能?

回答

3

它看起來像只在轉換器內部使用單個值,並且您只是在兩個值之間進行簡單切換,所以您可以直接在XAML中使用觸發器執行此操作。此方法還會切換到針對發票屬性的綁定,以便該屬性的任何更改通知都會導致觸發器更新。

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

謝謝,這工作像一個魅力。 – 2010-03-18 16:33:12

3

假設,因爲你要綁定到整個對象,你不能使用INotifyPropertyChanged的,你需要調用BindingExpression.UpdateTarget

微妙的微妙之處在於掌握了綁定表達式。這需要你有看法的一個相當成竹在胸:據我知道,要做到這一點的唯一方法是調用BindingOperations.GetBindingExpression,通過控制和屬性,其結合要更新,如:

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

即使在用戶界面上執行它仍然有點複雜。被綁定的元素是一個TreeView,我想在第二級刷新圖像。我已經添加了上面的UI代碼。 – 2010-03-18 11:27:12