小编Nix*_*Nix的帖子

展开时未显示WPF扩展器验证错误

使用MVVM.我有一个DataTemplate用于显示扩展器的扩展器,每个对象都有一些控件.

<DataTemplate>
    <Expander ExpandDirection="Down" IsExpanded="False">
        <Expander.Header>
            <TextBlock>
                <TextBlock.Text>
                    <MultiBinding StringFormat="Platform Group {0} {1}">
                        <Binding Path="PlatformGroupCode"/>
                        <Binding Path="PlatformGroupName"/>
                    </MultiBinding>
                </TextBlock.Text>
            </TextBlock>
        </Expander.Header>
        <vw:PlatformGroup HorizontalAlignment="Left"/>
    </Expander>
</DataTemplate>
Run Code Online (Sandbox Code Playgroud)

在该视图内部是绑定到这2个属性的2个文本框.我IDataErrorInfo在我的VM中使用进行验证,我在主应用程序资源中使用了一种样式,将错误消息显示为工具提示:

<Style TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=(Validation.Errors).CurrentItem.ErrorContent}"/>
        </Trigger>
    </Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)

添加新组时,2个属性具有默认值,这是无效的,因此我希望文本框为红色以提示用户输入数据.如果Expander的IsExpanded设置为true,则此方法有效.但如果它是假的,我必须扩展并更改其中一个文本框中的值,以便显示红色边框和工具提示.

我不想将扩展器设置为扩展,因为最终会有很多控件.扩展器扩展后如何才能显示红色边框?更好的是,是否有办法使新添加的扩展器扩展(当用户添加新组时,我将PlatformGroupviewModel添加到observablecollectionPlatformGroupviewModel中)?

编辑更多细节:顶级视图:

 <StackPanel Orientation="Vertical">
        <ScrollViewer VerticalScrollBarVisibility="Auto" MaxHeight="630">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="5*" />
                    <ColumnDefinition Width="5*" />
                </Grid.ColumnDefinitions>
                <StackPanel Orientation="Vertical" Grid.ColumnSpan="2" Grid.Row="1" HorizontalAlignment="Stretch"> …
Run Code Online (Sandbox Code Playgroud)

validation wpf expander mvvm idataerrorinfo

6
推荐指数
1
解决办法
1457
查看次数

标签 统计

expander ×1

idataerrorinfo ×1

mvvm ×1

validation ×1

wpf ×1