将复选框值作为参数传递给Converter

时间:2016-10-19 17:23:38

标签: c# wpf

我的WPF应用程序中有以下代码。我的xaml屏幕有2个控件...复选框按钮和一个文本框。

我需要能够触发MandatoryFieldConverter并将复选框值传递给它,以便我可以采取适当的操作。 我该如何做到这一点?

更新:我尝试使用multivValue转换器,但它不起作用:

我试过以下但我的转换器代码没有被触发。请指教。谢谢。

<CheckBox Name="chkPlaceholder" Command="{Binding PlaceholderCheckboxCommand}" VerticalAlignment="Center" IsChecked="{Binding IsSecurityPlaceholderChecked}" Style="{DynamicResource PlaceholderToggleButtonStyle}" ></CheckBox>

    <TextBox Name="txtUnitFactor" HorizontalAlignment="Left" Height="23" VerticalAlignment="Top" Width="180" TabIndex="1" Text="{Binding CommonSecurityAttributes.UnitFactor,NotifyOnSourceUpdated=True,  UpdateSourceTrigger=PropertyChanged}" Grid.Row="7" Grid.Column="4" >
                <TextBox.Background>

                    <MultiBinding Converter="{StaticResource CustomMandatoryFieldConverter}" UpdateSourceTrigger="PropertyChanged">
                            <Binding ElementName="chkPlaceholder" Path= "IsSecurityPlaceholderChecked" />
                            <Binding ElementName="txtUnitFactor" Path= "CommonSecurityAttributes.UnitFactor" />
                        </MultiBinding>
                </TextBox.Background>
                </TextBox>

MainWindowResources.cs:

<Converter:CustomMandatoryFieldBackgroundColourConverter x:Key="CustomMandatoryFieldConverter"/>

感谢。

MainWindowXaml.cs

<CheckBox 
    Name="chkPlaceholder" 
    Command="{Binding PlaceholderCheckboxCommand}" 
    VerticalAlignment="Center" 
    IsChecked="{Binding IsSecurityPlaceholderChecked}" 
    Style="{DynamicResource PlaceholderToggleButtonStyle}" 
    ></CheckBox>

<TextBox 
    HorizontalAlignment="Left" 
    Height="23" 
    VerticalAlignment="Top" 
    Width="180" 
    TabIndex="1" 
    Text="{Binding Price,NotifyOnSourceUpdated=True,  UpdateSourceTrigger=PropertyChanged}" 
    Background="{Binding CommonSecurityAttributes.UnitFactor,Converter ={StaticResource MandatoryFieldConverter}}" 
    Grid.Row="7" 
    Grid.Column="4"  />

C#转换器:

public class MandatoryFieldBackgroundColourConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string defaultBgColor = "BurlyWood";

            try
            {
                if (string.IsNullOrEmpty(value.ToString()))
                {
                    return defaultBgColor;
                }
                else
                {
                    return "LightGreen";
                }
            }
            catch (Exception)
            {
                return defaultBgColor;
            }
        }

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



public class CustomMandatoryFieldBackgroundColourConverter : IMultiValueConverter
{

    public object Convert(object[] value, Type targetType, object parameter, CultureInfo culture)
    {
        string defaultBgColor = "BurlyWood";
        string validationSuccessColor = "LightGreen";
        bool isPlaceHolderChecked = false;

 if (value == null || value != null && value.Length == 0)
            return defaultBgColor;

        try
        {
            bool result = bool.TryParse(value[1].ToString(), out isPlaceHolderChecked);

            if (isPlaceHolderChecked)
            {
                return validationSuccessColor;
            }

            if (string.IsNullOrEmpty(value.ToString()))
            {
                return defaultBgColor;
            }
            else
            {
                return validationSuccessColor;
            }
        }
        catch (Exception)
        {
            return defaultBgColor;
        }
    }

    public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

由于CheckBox IsChecked绑定到IsSecurityPlaceholderChecked,您应该让转换器直接绑定到视图模型的相同属性,而不是绑定到CheckBox( ,显然,没有IsSecurityPlaceholderChecked属性)

<CheckBox Name="chkPlaceholder" Command="{Binding PlaceholderCheckboxCommand}" VerticalAlignment="Center" IsChecked="{Binding IsSecurityPlaceholderChecked}" Style="{DynamicResource PlaceholderToggleButtonStyle}" ></CheckBox>

    <TextBox Name="txtUnitFactor" HorizontalAlignment="Left" Height="23" VerticalAlignment="Top" Width="180" TabIndex="1" Text="{Binding CommonSecurityAttributes.UnitFactor,NotifyOnSourceUpdated=True,  UpdateSourceTrigger=PropertyChanged}" Grid.Row="7" Grid.Column="4" >
    <TextBox.Background>

        <MultiBinding Converter="{StaticResource CustomMandatoryFieldConverter}" UpdateSourceTrigger="PropertyChanged">
            <Binding Path= "IsSecurityPlaceholderChecked" />
            <Binding Path= "CommonSecurityAttributes.UnitFactor" />
        </MultiBinding>
    </TextBox.Background> 
</TextBox>