带 OR 语句的 DataTrigger

DataTrigger with OR statement

我想合并以下代码,可能对满足条件的多个值使用 OR 语句?它变得非常重复,他们必须是一种更好的写法

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

如能帮助巩固这一点,我们将不胜感激

我建议创建一个 IValueConverter,将 Type 字符串转换为 Source 以用于您的 <Image>。这样每个模板只能有一个图像,而不是有许多折叠和不可见的图像。您将拥有更少的代码和更好的性能。

用以下内容替换所有内容:

<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();
    }
}