2012-06-11 21 views
6

MVVMを使用しています。私はDataTemplateを持っています。これはオブジェクトごとにいくつかのコントロールを持つエクスパンダを表示するために使用しています。展開時にWPFエキスパンダーの検証エラーが表示されない

<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> 

このビューの内部には、2つのテキストボックスが2つのプロパティにバインドされています。私は、検証を行うために私のVMでIDataErrorInfoを使用していると私は私のメインのアプリケーション・リソースにスタイルをしたツールチップとしてエラーメッセージを表示するには:

<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> 

新しいグループが追加されると2つのプロパティはデフォルト値があり、その無効なので、テキストボックスを赤色にしてユーザーにデータを入力させるようにします。 ExpanderのIsExpandedがtrueに設定されている場合、これは機能します。しかし、それが偽であれば、赤い枠線とツールチップが表示されるように、テキストボックスの1つで値を変更して値を変更する必要があります。

最終的にかなりのコントロールが存在するため、エクスパンダを展開するように設定したくありません。エキスパンダーが展開されたらすぐに赤い枠線を表示するにはどうすればよいですか?さらに、新しく追加されたエクスパンダを展開する方法はありますか(ユーザーが新しいグループを追加すると、PlatformGroupviewModelをPlatformGroupviewModelsのobservablecollectionに追加しています)。

EDIT詳細: トップレベルのビュー:

<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ビュー:

<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> 
+0

あなたは、 '= true'をあなたのTextBoxのバインディングに設定されValidatesOnDataErrorsを持っていますか? – Rachel

+0

はい(2つのテキストボックスのコードは表示していませんが)はい。 IsExpanded = trueの場合、検証エラーが表示されます。これはエキスパンダーの既知の問題だと思われますが、公開されているakjoshiの既知の解決策はこのケースでは機能していないようです。 – Nix

答えて

3

としてthisあたりのポスト、この問題を解決する必要がありますAdornerDecorator中にあなたExpanderコンテンツを包む -

<DataTemplate> 
    <Expander ExpandDirection="Down" IsExpanded="False"> 
     <Expander.Header> 
      ... 
     </Expander.Header> 

     <AdornerDecorator> 
      <vw:PlatformGroup HorizontalAlignment="Left"/> 
     </AdornerDecorator> 

    </Expander> 
</DataTemplate> 

は別のSOこれを確認したスレッド - Issue with WPF validation(IDataErrorInfo) and tab focusing

他のいくつかの回避策も、この接続バグに記載されている -

TabControl doesn't display Validation error information correctly when switching tabs back and forth

+0

私は試しましたが、それは違いはありませんでした。エキスパンダー自体は別のエキスパンダーにネストされていますが、アドオンデコレーターを置いてもそれは役に立ちませんでした。私はもう少しコードで私の質問を更新します。 – Nix

+3

私はついにこれに戻りました。 AdornerDecoratorを置く場所を正確に管理しなければならないようです。私のPlatformGroupビューの中には、グリッドが内部にあり、テキストボックスがグリッドの内側にあるスタックパネルがありました。エキスパンダーの内容全体ではなく、AdornerDecoratorをグリッドの周りに配置すると、それが機能しました。 – Nix

+0

あなたが解決策を見つけたらうれしいです。はい、時々WPFで扱いにくくなり、その背後にある厳しい理由を理解するのは難しいです。 – akjoshi

関連する問題