解耦 IValueConverter(Disable/Enable 一个 WPF 转换器)

Decouple IValueConverter (Disable/Enable a WPF Converter)

这是一种启用和禁用 WPF 转换器的方法吗?以编程方式或直接从 WPF 将复选框控件绑定到它。

我的应用程序中有这个文本框和复选框:

当复选框未选中时,我可以输入任何数值,但当我选中复选框时,我想启用此转换器:

<TextBox
Grid.Row="1"
Grid.Column="1"
Margin="0,0,10,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
MaxLength="41"
Text="{
    Binding Payload, 
    Mode=TwoWay, 
    Converter={StaticResource HexStringConverter}, 
    UpdateSourceTrigger=PropertyChanged}" 
/>

此外,这是转换器 class:

public class HexStringConverter : IValueConverter
    {
        private string lastValidValue;
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string ret = null;

            if (value != null && value is string)
            {
                var valueAsString = (string)value;
                var parts = valueAsString.ToCharArray();
                var formatted = parts.Select((p, i) => (++i) % 2 == 0 ? String.Concat(p.ToString(), " ") : p.ToString());
                ret = String.Join(String.Empty, formatted).Trim();
            }

            return ret;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            object ret = null;
            if (value != null && value is string)
            {
                var valueAsString = ((string)value).Replace(" ", String.Empty).ToUpper();
                ret = lastValidValue = IsHex(valueAsString) ? valueAsString : lastValidValue;
            }

            return ret;
        }


        private bool IsHex(string text)
        {
            var reg = new System.Text.RegularExpressions.Regex(@"^[0-9A-Fa-f\[\]]*$");
            return reg.IsMatch(text);
        }
    }

像往常一样,在 WPF 中,有很多方法可以做到这一点。

一种方法是使用触发器更改给 Text 的绑定,例如:

<TextBlock ....>
    <TextBlock.Style>
        <Style TargetType="TextBlock">
            <Setter Property="Text" Value="{Binding Payload}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsChecked, ElementName=NameOfCheckBox}" Value="True">
                    <Setter Property="Text"
                            Value="{Binding Payload, Converter={StaticResource HexToStringConverter}}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

另一种方法是使用 IMultiValueConverter:

public class HexStringConverter : IMultiValueConverter
{
    public object Convert (object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values.Length != 2 ||
            values[0] is not string str ||
            values[1] is not bool isEnabled)
        {
            return DependencyProperty.UnsetValue;
        }
 
        if (isEnabled)
        {
            // Do the actual conversion
        }
        else
        {
            return str;
        }
    }
}

然后:

<TextBlock ...>
    <TextBlock.Text>
        <MultiBinding Converter="{StaticResource HexToStringConverter}">
            <Binding Path="Payload"/>
            <Binding Path="IsChecked" ElementName="NameOfCheckBox"/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>