Using MVVM. I have a DataTemplate
that I use to display an expander with some controls in each object.
<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>
Inside this view, there are 2 text fields attached to these 2 properties. I use IDataErrorInfo
in my virtual machine for checking, and I have a style in my main application resources for displaying error messages as prompts:
<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>
When adding a new group 2, the properties have default values, which is invalid, so I want the text fields to be red to prompt the user to enter data. This works if Expander IsExpanded is set to true. But if it is a lie, I need to expand AND change the value in one of the text fields to display a red frame and a tooltip.
I do not want the expander to expand, because in the end there will be quite a lot of controls. How can I get a red frame to show as soon as the expander expands? Even better, is there a way to expand the added extender (when the user adds a new group, I add PlatformGroupviewModel to the observablecollection
for PlatformGroupviewModels)?
EDIT more: top level view:
<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"> <Expander ExpandDirection="Down" IsExpanded="True" Header="Header" HorizontalAlignment="Stretch" Name="expHeader" VerticalAlignment="Top"> <vw:Header DataContext="{Binding HeaderVM}"/> </Expander> <Expander ExpandDirection="Down" IsExpanded="True" Header="Platform Groups" HorizontalAlignment="Stretch" Name="expPlatformGroups" VerticalAlignment="Top"> <AdornerDecorator> <vw:PlatformGroups DataContext="{Binding PlatformGroupsVM}"/> </AdornerDecorator> </Expander> </StackPanel> </Grid> </ScrollViewer> </StackPanel>
PlatformGroups view:
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch" Margin="10,10,10,10"> <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" Margin="0,10"> <Label Content="Number of platform groups" VerticalAlignment="Center"/> <vw:IntegerInput MinValue="0" MaxValue="50" MaxLength="2" Text="{Binding Path=NumPlatformGroups, Mode=TwoWay,ValidatesOnDataErrors=True}" HorizontalAlignment="Left" VerticalAlignment="Center"/> </StackPanel> <ItemsControl IsTabStop="False" ItemsSource="{Binding PlatformGroups}" Margin="20,10" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> <ItemsControl.ItemTemplate> <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> <AdornerDecorator> <vw:PlatformGroup HorizontalAlignment="Left"/> </AdornerDecorator> </Expander> </DataTemplate> </ItemsControl.ItemTemplate> <ItemsControl.Template> <ControlTemplate> <Border BorderThickness="{TemplateBinding Border.BorderThickness}" Padding="{TemplateBinding Control.Padding}" BorderBrush="{TemplateBinding Border.BorderBrush}" Background="{TemplateBinding Panel.Background}" SnapsToDevicePixels="True"> <ScrollViewer VerticalScrollBarVisibility="Auto" MaxHeight="400" CanContentScroll="True" Padding="{TemplateBinding Control.Padding}" Focusable="False"> <ItemsPresenter SnapsToDevicePixels="{TemplateBinding UIElement.SnapsToDevicePixels}" /> </ScrollViewer> </Border> </ControlTemplate> </ItemsControl.Template> </ItemsControl> </StackPanel>