C# 将复选框值作为参数传递给转换器
我的WPF应用程序中有以下代码。我的xaml屏幕有2个控件…复选框按钮和一个文本框 我需要能够触发MandatoryFieldConverter并将复选框值传递给它,以便采取适当的操作。 请问我如何做到这一点 更新:我尝试使用多值转换器,但不起作用: 我尝试了以下操作,但我的转换器代码没有被触发。请告知。谢谢C# 将复选框值作为参数传递给转换器,c#,wpf,C#,Wpf,我的WPF应用程序中有以下代码。我的xaml屏幕有2个控件…复选框按钮和一个文本框 我需要能够触发MandatoryFieldConverter并将复选框值传递给它,以便采取适当的操作。 请问我如何做到这一点 更新:我尝试使用多值转换器,但不起作用: 我尝试了以下操作,但我的转换器代码没有被触发。请告知。谢谢 <CheckBox Name="chkPlaceholder" Command="{Binding PlaceholderCheckboxCommand}" VerticalAlig
<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>
谢谢您的帮助。既然您的
复选框已选中已绑定到IsSecurityPlaceholderChecked
,那么您应该让转换器直接绑定到视图模型的同一属性,而不是复选框(显然,它没有IsSecurityPlaceholderChecked
属性)
您是说要将两个值传递给转换器,CommonSecurityAttributes.UnitFactor
和IsSecurityPlaceholderChecked
?如果是这样,您需要的是多转换器(IMultiValueConverter),而不是常规的值转换器。什么是CustomMandaryFieldConverter?你能告诉我它的代码吗?当您说它“不起作用”时,您能确切地告诉我这是什么意思吗?复选框没有IsSecurityPlaceholderChecked
属性。而是在复选框中将IsChecked
绑定到IsSecurityPlaceholderChecked
。您需要绑定IsChecked
,或者从视图模型中绑定相同的IsSecurityPlaceholderChecked
。调试时请检查控制台。你可能有绑定错误。注意马特——在你的@MattBurland中去掉两个元素名。我想他需要的不是颜色,而是画笔<代码>新SolidColorBrush(Colors.BurlyWood)代码>等。
<Converter:CustomMandatoryFieldBackgroundColourConverter x:Key="CustomMandatoryFieldConverter"/>
<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" />
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();
}
}
<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>