【6月日新计划22】WPF入门-Converters

141 阅读1分钟

1.Converters

1.1 Create

根據功能命名,繼承IValueConverter,並重寫裡面的方法

    public class InverseBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (targetType != typeof(bool))
                throw new InvalidOperationException("The target must be a boolean");

            return !(bool)value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return !(bool)value;
        }
    }

1.2 Import

在主程序App.xaml導入

    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <materialDesign:BundledTheme
                    BaseTheme="Light"
                    PrimaryColor="DeepPurple"
                    SecondaryColor="Lime" />
                <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <convert:InverseBooleanConverter x:Key="InverseBooleanConverter" />
        </ResourceDictionary>
    </Application.Resources>

1.3 Use

 IsEnabled="{Binding ElementName=StationToggleBtn, 
                     Path=IsChecked, 
                     Converter={StaticResource InverseBooleanConverter}, 
                     Mode=TwoWay}"

2. 下載輪子

Dll Website : www.dllme.com/

不要自己造輪子,又丑又難用

图片.png