WPF中对组合框选择更改的验证 [英] Validation in wpf on combobox selection change

查看:42
本文介绍了WPF中对组合框选择更改的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文本框和一个组合框.如果在组合框中选择了某些内容,则该文本框应成为必填项.我该怎么做...?请帮助我

谢谢
Kunjammu

I have a textbox and a combobox. if something selected in the combobox, the textbox should become mandatory. How can i do that...?plz help me

Thanks
Kunjammu

推荐答案

我最初会将editbox enabled属性设置为false.
一旦选择在组合中发生更改,无论是通过绑定还是通过捕获selectionchanged事件,您都可以启用编辑框.
这还不意味着要进行任何验证,但是应该为用户应该在编辑框中填写的内容提供直观的提示.
然后,在确定"按钮处理程序中,您实际上可以通过对编码进行编码来创建验证.

这是最简单的方法,如果使用模型视图viewmodel方法,还有其他方法可以做到这一点.

更复杂的绑定示例:

I would initially set the editbox enabled property to false.
Once the selection changes in the combo, either through binding or via capturing the selectionchanged event you can enable the editbox.
This does not yet imply any validation but it should give a visual clue to the user he is supposed to fill in the edit box.
Then in the ok button handler you can actually create the validation by just coding it.

This is the most simple approach, if you use the model view viewmodel approach there are other methods to do this.

An example of more complex binding :

<Style x:Key="NonEmptyTextBlock" TargetType="TextBlock">
                <Setter Property="Visibility" Value="Visible"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Text}" Value="">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}},Path=IsSelected}" Value="True"/>
                            <Condition Binding="{Binding AF}" Value="False"/>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>



第一个触发器确保如果应用了此样式的文本块为空,则将其折叠.
第二个触发器更为复杂,并使用2个条件.第一个条件是必须选择此文本块所在的listboxitem,第二个条件是条件AF必须为false,这是我的视图模型中的变量.绑定可能有点复杂,但是有很多教程和书籍可以教这个.



The first trigger makes sure that if the textblock,where this style is applied on, is empty, then it is collapsed.
The second trigger is more complex and uses 2 conditions. The first condition is that the listboxitem where this textblock resides in must be selected, the second condition is that the condition AF must be false, this is a variable in my viewmodel. Binding can be a little complex but there are plenty of tutorials and books to teach this.


private bool ValidateFields()
{


if(ComboBox.SelectedIndex> = 0)
{
在此处为文本框编写验证.

}



}
private bool ValidateFields()
{


if(ComboBox.SelectedIndex>=0)
{
write validation for textbox here.

}



}


这篇关于WPF中对组合框选择更改的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆