加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 验证ItemsControl中的项目

发布时间:2020-12-15 08:47:36 所属栏目:百科 来源:网络整理
导读:我正在开发一个 WPF应用程序,在一个窗口中我使用了WPF工具包中的向导组件.在这个向导中,我正在创建一个新人.在第二步中,我使用枚举作为可能的联系类型的来源(例如电话,电子邮件……). 这是我在XAML中的向导页面: xctk:WizardPage x:Name="NewContactPage" P
我正在开发一个 WPF应用程序,在一个窗口中我使用了WPF工具包中的向导组件.在这个向导中,我正在创建一个新人.在第二步中,我使用枚举作为可能的联系类型的来源(例如电话,电子邮件……).

这是我在XAML中的向导页面:

<xctk:WizardPage x:Name="NewContactPage" PageType="Interior"
                Title="Contacts" Style="{DynamicResource NewContactPage}"
                CanCancel="True" CanFinish="False"
                Loaded="NewContactPage_Loaded" 
                PreviousPage="{Binding ElementName=NewPersonPage}">
    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Top">
        <control:DataLoader x:Name="ctrNewContactLoader" />
        <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Top" Orientation="Vertical">
            <ItemsControl ItemsSource="{Binding Path=Person.PersonContacts,Mode=TwoWay,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Window}}"
                                      Name="icContacts">
                <ItemsControl.ItemTemplate>
                    <ItemContainerTemplate>
                        <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Top" Orientation="Vertical"


                        Margin="5" Background="WhiteSmoke">
                        <CheckBox IsChecked="{Binding Path=IsValid}" 
                                              Content="{Binding Path=ContactType.Description}"
                                              Name="cbContactVisible"/>

                        <Grid HorizontalAlignment="Stretch" VerticalAlignment="Top"
                                          Visibility="{Binding ElementName=cbContactVisible,Path=IsChecked,Converter={StaticResource BooleanToVisibilityConverter}}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="auto" />
                            </Grid.RowDefinitions>

                            <TextBox Grid.Row="0" Grid.Column="0"
                                                 HorizontalAlignment="Stretch" MaxLength="64"
                                                 Name="txtContactValue"
                                                 Text="{Binding Path=Contact,ValidatesOnDataErrors=True,ValidatesOnNotifyDataErrors=True,ValidatesOnExceptions=True}" />
                        </Grid>
                    </StackPanel>
                </ItemContainerTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </StackPanel>
</Grid>

ItemsControl的源是PersonContactModel类的List:

public class PersonContactModel : BaSEObjectModel
{
    public PersonContactModel()
    {
        this.Created = DateTime.Now;
        this.Updated = DateTime.Now;

        this.IsValid = true;

        this.ContactType = new ContactTypeModel();
    }

    public string Contact { get; set; }
    public ContactTypeModel ContactType { get; set; }
    public DateTime Created { get; set; }

    public int Id { get; set; }
    public bool IsValid { get; set; }
    public DateTime Updated { get; set; }

    public override string this[string columnName]
    {
        get
        {
            string retVal = string.Empty;
            switch (columnName)
            {
                case "Contact":
                    retVal = base.Concat(base.RequeiredField(this.Contact),base.MinLength(this.Contact,5),base.MaxLength(this.Contact,62));
                    break;
            }

            return retVal;
        }
    }
}

基类实现IDataErrorInfo接口,其中包含有关Contact属性的验证信息.

所需的行为是,如果选中该复选框,则它是可见的网格,其中包含用于输入联系人的字段,否则不是.仅当选定的联系类型有效时,才能看到按钮下一步.此功能正在尝试在app.xaml中完成以下样式:

<Style TargetType="xctk:WizardPage" x:Key="NewContactPage">
    <Setter Property="NextButtonVisibility" Value="Hidden" />
    <Style.Triggers>
        <MultiDataTrigger>
            <MultiDataTrigger.Conditions>
                <Condition Binding="{Binding Path=(Validation.HasError),ElementName=txtContactValue}" Value="False" />
            </MultiDataTrigger.Conditions>
            <Setter Property="NextButtonVisibility" Value="Visible" />
        </MultiDataTrigger>
    </Style.Triggers>
</Style>

不幸的是,下一步的按钮是不可见的,即使它要求新人的各种联系并且将满足有效输入的所有条件.

怎么了?哪里出错?

解决方法

你试图以一种不太好的方式实现你想要的东西.此特定代码中的错误是因为您从样式触发器引用元素“txtContactValue”,并且样式根本不知道该元素是什么.顺便说一句,如果你在调试代码时查看输出窗口,我打赌你会在那里看到这个错误.

现在,即使您将尝试引用没有样式的“txtContactValue”,如下所示:

NextButtonVisibility="{Binding ElementName=txtContactValue,Path=(Validation.HasError),Converter={StaticResource BooleanToVisibilitConverter}}"

它不起作用,因为txtContactValue在不同的范围内.但是你不应该在第一时间这样做!您有一个数据模型,这是控制数据是否有效的模型.只需在模型中添加一些属性,指示您在此向导页面上创建的数据是否有效(如PersonContact.IsValid),然后您可以继续下一页,并绑定到此属性.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读