WPF call method from DataTrigger

Can I use a wild card or call a development method if a DataTrigger is to be applied?

Currently, my DataList is bound to an IEnumerable that contains file names, and I want the file names to be gray if the file extension starts with "old"

My non-working xaml footnote looks something like this:

<DataTemplate.Triggers> <DataTrigger Binding="{Binding}" Value="*.old*"> <Setter TargetName="FileName" Property="Foreground" Value="Gray"/> </DataTrigger> </DataTemplate.Triggers> 

The only workable solution that I could come up with was to insert a new property of the view model that contains this logic, but I would like to avoid changing the view model, if possible.

+7
source share
1 answer

The answer to both questions is yes .... in a workaround

If you use Binding Converter, you can pass a parameter to it and return a boolean to it, which will be an effective way to do what you describe.

 <DataTemplate.Triggers> <DataTrigger Binding="{Binding Path=., Converter={StaticResource myFileExtensionConverter}, ConverterParameter=old}" Value="True"> <Setter TargetName="FileName" Property="Foreground" Value="Gray"/> </DataTrigger> </DataTemplate.Triggers> 

where the converter will look something like this.

  public class MyFileExtensionConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { Boolean returnValue = false; String fileExtension = parameter as String; String fileName = value as String; if (String.IsNullOrEmpty(fileName)) { } else if (String.IsNullOrEmpty(fileExtension)) { } else if (String.Compare(Path.GetExtension(fileName), fileExtension, StringComparison.OrdinalIgnoreCase) == 0) { returnValue = true; } return returnValue; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { return value; } } 

basically, when the file extension matches you, you get "true" which fires the trigger.

+7
source

All Articles