WPF Trigger based on Object Type

前端 未结 5 939
自闭症患者
自闭症患者 2020-12-13 17:27

Is there a way to do a comparison on object type for a trigger?


         


        
5条回答
  •  抹茶落季
    2020-12-13 18:26

    Why not just use a converter that takes an object and returns a string of the object type?

    Binding="{Binding SelectedItem, Converter={StaticResource ObjectToTypeString}}"

    and define the converter as:

    public class ObjectToTypeStringConverter : IValueConverter
    {
        public object Convert(
         object value, Type targetType,
         object parameter, System.Globalization.CultureInfo culture)
        {
            return value.GetType().Name;            
        }
    
        public object ConvertBack(
         object value, Type targetType,
         object parameter, System.Globalization.CultureInfo culture)
        {
            // I don't think you'll need this
            throw new Exception("Can't convert back");
        }
    }
    

    You'll need to declare the static resource somewhere in your xaml:

    
        
    
    

    Where 'convs' in this case is the namespace of where the converter is.

    Hope this helps.

提交回复
热议问题