2017-03-22 49 views
4

我想整合下面的代碼,可能使用OR語句來實現可以滿足條件的多個值?它變得非常重複的,他們必須寫這篇帶OR語句的DataTrigger

    <DataGridTemplateColumn Header="" Width="28"> 
         <DataGridTemplateColumn.CellTemplate> 
          <DataTemplate> 
           <StackPanel> 
            <Image Name="imageEXCEL" Source="/Images/excel.png" Visibility="Collapsed" Width="16" Height="16" /> 
            <Image Name="imageWORD" Source="/Images/word.png" Visibility="Collapsed" Width="16" Height="16" /> 
            <Image Name="imageACCESS" Source="/Images/access.png" Visibility="Collapsed" Width="16" Height="16" /> 
            <Image Name="imagePOWERPOINT" Source="/Images/powerpoint.png" Visibility="Collapsed" Width="16" Height="16" /> 
            <Image Name="imagePUBLISHER" Source="/Images/publisher.png" Visibility="Collapsed" Width="16" Height="16" /> 
            <Image Name="imageONENOTE" Source="/Images/onenote.png" Visibility="Collapsed" Width="16" Height="16" /> 
            <Image Name="imageOUTLOOK" Source="/Images/outlook.png" Visibility="Collapsed" Width="16" Height="16" /> 
            <Image Name="imageVISIO" Source="/Images/visio.png" Visibility="Collapsed" Width="16" Height="16" /> 
            <Image Name="imagePROJECT" Source="/Images/project.png" Visibility="Collapsed" Width="16" Height="16" /> 
            <Image Name="imagePDF" Source="/Images/pdf.png" Visibility="Collapsed" Width="16" Height="16" /> 
           </StackPanel> 
           <DataTemplate.Triggers> 
            <DataTrigger Binding="{Binding Path=Type}" Value="pdf" > 
             <Setter TargetName="imagePDF" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="xlr"> 
             <Setter TargetName="imageEXCEL" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="xlsx"> 
             <Setter TargetName="imageEXCEL" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="xlsm"> 
             <Setter TargetName="imageEXCEL" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="xltx"> 
             <Setter TargetName="imageEXCEL" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="xltm"> 
             <Setter TargetName="imageEXCEL" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="xls"> 
             <Setter TargetName="imageEXCEL" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="xlt"> 
             <Setter TargetName="imageEXCEL" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="xlm"> 
             <Setter TargetName="imageEXCEL" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="xlsb"> 
             <Setter TargetName="imageEXCEL" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="xla"> 
             <Setter TargetName="imageEXCEL" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="xlam"> 
             <Setter TargetName="imageEXCEL" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="xll"> 
             <Setter TargetName="imageEXCEL" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="xlw"> 
             <Setter TargetName="imageEXCEL" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="ppt"> 
             <Setter TargetName="imagePOWERPOINT" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="pot"> 
             <Setter TargetName="imagePOWERPOINT" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="pps"> 
             <Setter TargetName="imagePOWERPOINT" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="pptx"> 
             <Setter TargetName="imagePOWERPOINT" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="pptm"> 
             <Setter TargetName="imagePOWERPOINT" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="potx"> 
             <Setter TargetName="imagePOWERPOINT" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="potm"> 
             <Setter TargetName="imagePOWERPOINT" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="ppam"> 
             <Setter TargetName="imagePOWERPOINT" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="ppsx"> 
             <Setter TargetName="imagePOWERPOINT" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="ppsm"> 
             <Setter TargetName="imagePOWERPOINT" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="sldx"> 
             <Setter TargetName="imagePOWERPOINT" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="sldm"> 
             <Setter TargetName="imagePOWERPOINT" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="adp"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="adn"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="accdb"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="accdw"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="accdc"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="accda"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="accdr"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="accdt"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="mdb"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="mda"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="mdw"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="mdf"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="mde"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="accde"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="mam"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="mad"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="maq"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="mar"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="mat"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="maf"> 
             <Setter TargetName="imageACCESS" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="doc"> 
             <Setter TargetName="imageWORD" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="dot"> 
             <Setter TargetName="imageWORD" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="wbk"> 
             <Setter TargetName="imageWORD" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="docx"> 
             <Setter TargetName="imageWORD" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="docm"> 
             <Setter TargetName="imageWORD" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="dotx"> 
             <Setter TargetName="imageWORD" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="dotm"> 
             <Setter TargetName="imageWORD" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="docb"> 
             <Setter TargetName="imageWORD" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="pub"> 
             <Setter TargetName="imagePUBLISHER" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="one"> 
             <Setter TargetName="imageONENOTE" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="oft"> 
             <Setter TargetName="imageOUTLOOK" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="pst"> 
             <Setter TargetName="imageOUTLOOK" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="obi"> 
             <Setter TargetName="imageOUTLOOK" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="olm"> 
             <Setter TargetName="imageOUTLOOK" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="ost"> 
             <Setter TargetName="imageOUTLOOK" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="iaf"> 
             <Setter TargetName="imageOUTLOOK" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="oab"> 
             <Setter TargetName="imageOUTLOOK" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="prf"> 
             <Setter TargetName="imageOUTLOOK" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="vsdx"> 
             <Setter TargetName="imageVISIO" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="vsd"> 
             <Setter TargetName="imageVISIO" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="vss"> 
             <Setter TargetName="imageVISIO" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="vst"> 
             <Setter TargetName="imageVISIO" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="vsx"> 
             <Setter TargetName="imageVISIO" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="vdx"> 
             <Setter TargetName="imageVISIO" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="vtx"> 
             <Setter TargetName="imageVISIO" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="mpp"> 
             <Setter TargetName="imagePROJECT" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="mpt"> 
             <Setter TargetName="imagePROJECT" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 
            <DataTrigger Binding="{Binding Path=Type}" Value="mpd"> 
             <Setter TargetName="imagePROJECT" Property="Visibility" Value="Visible"/> 
            </DataTrigger> 


           </DataTemplate.Triggers> 
          </DataTemplate> 

         </DataGridTemplateColumn.CellTemplate> 
        </DataGridTemplateColumn> 

與鞏固任何幫助的一個更好的辦法將大大理解

回答

5

改爲如下的一切:

<Window.Resources> 
    <!-- somewhere in resources --> 
    <wpfApplication1:TypeToImageConverter x:Key="typeToImage" /> 
</Window.Resources> 
<Image Source="{Binding Type, Converter={StaticResource typeToImage}}"/> 

而且使用轉換器:

public class TypeToImageConverter : IValueConverter { 
    static readonly HashSet<string> _pdfExtensions = new HashSet<string>(new[] {"pdf"}); 
    static readonly HashSet<string> _excelExtensions = new HashSet<string>(new[] { "xlr", "xlsx", "xlsm" }); 
    // and so on 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { 
     var ext = value as string;    
     string relativePath = null; 
     if (ext != null) { 
      ext = ext.ToLowerInvariant(); 
      if (_pdfExtensions.Contains(ext)) 
       relativePath = "/Images/pdf.png"; 
      else if (_excelExtensions.Contains(ext)) 
       relativePath = "/Images/excel.png"; 
      // and so on 
     } 
     if (relativePath != null) { 
      var bmp = new BitmapImage(); 
      bmp.BeginInit(); 
      bmp.UriSource = new Uri(relativePath, UriKind.Relative); 
      bmp.EndInit(); 
      return bmp; 
     } 
     return null; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { 
     throw new NotImplementedException(); 
    } 
} 
+0

工作!非常感謝 – SlopTonio

1

我建議創建一個IValueConverter一種轉換Type字符串變爲Source爲您的<Image>。這樣,每個模板只能有一個圖像,而不是有許多摺疊和不可見的圖像。你會有更少的代碼和更好的性能。