我有两个组合框- cbo_client_pay_method和cbo_terms
其中一个cbo_client_pay_method项目(在账户上)需要cbo_terms (30天等...)为了可见,否则它会折叠,我已经在cbo_payment_type_SelectionChanged事件中设置了此功能。
我已经实现了一个测试cbo是否不为空的validationRule &&如果cbo <0 (选择了某项内容),则可以正常工作。
这一切都很好用,除非cbo崩溃,验证仍然会触发!
如果元素被折叠,我可以停止validationRule吗?
<StackPanel Name="sp_account" Orientation="Horizontal" VerticalAlignment="Center">
<Label Content="Payment" Style="{StaticResource formLabel}"/>
<Grid>
<ComboBox Name="cbo_client_pay_method" Style="{StaticResource reminder_cbo}" SelectionChanged="cbo_client_payMethod_SelectionChanged" Validation.ErrorTemplate="{StaticResource validationTemplate}">
<ComboBox.SelectedValue>
<Binding Path="client_payment_type_id" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True">
<Binding.ValidationRules>
<local:ValidCbo ErrorMessage="Select A Payment Type" />
</Binding.ValidationRules>
</Binding>
</ComboBox.SelectedValue>
</ComboBox>
<TextBlock Name="txtSelectPayMethod" Text="Please Select A Payment Method..." Style="{StaticResource cbo_overlay}" />
</Grid>
</StackPanel>
<StackPanel Name="sp_terms" Orientation="Horizontal" VerticalAlignment="Center">
<Label Content="Terms" Style="{StaticResource formLabel}"/>
<Grid>
<ComboBox Name="cbo_terms" Style="{StaticResource reminder_cbo}" SelectionChanged="cbo_terms_SelectionChanged" Validation.ErrorTemplate="{StaticResource validationTemplate}">
<ComboBox.SelectedValue>
<Binding Path="terms_id" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True">
<Binding.ValidationRules>
<local:ValidCbo ErrorMessage="Select Payment Terms" />
</Binding.ValidationRules>
</Binding>
</ComboBox.SelectedValue>
</ComboBox>
<TextBlock Name="txtSelectTerms" Text="Please Select Payment Terms..." Style="{StaticResource cbo_overlay}" />
</Grid>
</StackPanel>public class ValidCbo : ValidationRule
{
private string _errorMessage;
public string ErrorMessage
{
get { return _errorMessage; }
set { _errorMessage = value; }
}
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
//if (this.ErrorMessage.Contains("Master") |)
if (value == null )
{
// value = null
return new ValidationResult(false, this.ErrorMessage);
}
else
{
// Not null
int selectedValue = (int)value;
if (selectedValue < 0)
{
return new ValidationResult(false, this.ErrorMessage);
}
else
{
return ValidationResult.ValidResult;
}
}
}
}发布于 2011-04-21 21:37:51
您可以应用仅在ComboBox可见时才绑定值的样式:
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}">
<Style.Triggers>
<Trigger Property="Visibility" Value="Visible">
<Setter Property="SelectedValue">
<Setter.Value>
<Binding Path="terms_id" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="True">
<Binding.ValidationRules>
<local:ValidCbo ErrorMessage="Select Payment Terms" />
</Binding.ValidationRules>
</Binding>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ComboBox.Style>您不能在ComboBox本身中设置SelectedValue,否则它会覆盖样式。
https://stackoverflow.com/questions/5743127
复制相似问题