2017-11-24 1 views
1

스타일 상자를 통해 DataGrid 열의 머리글에 추가되는 필터 상자를 가져 오는 데 문제가 있습니다. 모든 항목을 정렬하지 않으면 문제가 발생하지 않지만 정렬하는 순간 더 이상 입력 된 내용이 표시되지 않고 한 문자 만 필터링 할 수 있습니다 (필터를 지울 수 없음) TextChanged 이벤트를 삭제하고 트리거 할 문자가 없습니다.)WPF DataGrid의 정렬 및 필터링 관련 문제

열이 정렬 될 때 DataGridColumnHeader 인스턴스에서 시작하여 TextBox가 시각적 트리에서 발견되지 않는 것은 이상한 일입니다. 찾을 수 있으면주기가 끝나면 텍스트를 다시 설정할 수 있으며 문제는 해결 될 것입니다. 의 정의에 특별한

<Page.Resources> 
    <Style x:Key="Filter" TargetType="TextBox"> 
     <EventSetter Event="TextChanged" Handler="Filter_TextBox_TextChanged"/> 
    </Style> 
    <Style TargetType="{x:Type DataGrid}"> 
     <!-- Some visual styling like margins and colors on the grid --> 
     <Style.Resources> 
      <Style TargetType="{x:Type DataGridColumnHeader}"> 
       <!-- Some visual styling like margins and colors on the header --> 
       <Setter Property="ContentTemplate"> 
        <Setter.Value> 
         <DataTemplate> 
          <StackPanel> 
           <Grid> 
            <Grid.RowDefinitions> 
             <RowDefinition Height="Auto"/> 
             <RowDefinition Height="Auto"/> 
            </Grid.RowDefinitions> 
            <Grid.ColumnDefinitions> 
             <ColumnDefinition Width="*"/> 
             <ColumnDefinition Width="Auto"/> 
             <ColumnDefinition Width="Auto"/> 
            </Grid.ColumnDefinitions> 
            <TextBlock Grid.Column="0" Grid.Row="0" Margin="10,0,10,0" TextWrapping="WrapWithOverflow" Text="{Binding}"/> 
            <fa:FontAwesome Grid.Column="1" Grid.Row="0" Icon="LongArrowUp" Foreground="#dbdbdb" HorizontalAlignment="Right" VerticalAlignment="Bottom"/> 
            <fa:FontAwesome Grid.Column="2" Grid.Row="0" Margin="0,0,10,0" Icon="LongArrowDown" Foreground="#dbdbdb" HorizontalAlignment="Right" VerticalAlignment="Bottom"/> 
            <StackPanel Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="1" Margin="0,10,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Background="#dbdbdb"> 
             <TextBox Style="{StaticResource Filter}" /> 
            </StackPanel> 
           </Grid> 
          </StackPanel> 
         </DataTemplate> 
        </Setter.Value> 
       </Setter> 
       <Style.Triggers> 
        <Trigger Property="SortDirection" Value="Ascending"> 
         <Setter Property="Background" Value="#F0F0F0"/> 
         <Setter Property="ContentTemplate"> 
          <Setter.Value> 
           <DataTemplate> 
            <StackPanel> 
             <Grid> 
              <Grid.RowDefinitions> 
               <RowDefinition Height="Auto"/> 
               <RowDefinition Height="Auto"/> 
              </Grid.RowDefinitions> 
              <Grid.ColumnDefinitions> 
               <ColumnDefinition Width="*"/> 
               <ColumnDefinition Width="Auto"/> 
              </Grid.ColumnDefinitions> 
              <TextBlock Grid.Column="0" Grid.Row="0" Margin="5,0,10,0" TextWrapping="WrapWithOverflow" Text="{Binding}"/> 
              <fa:FontAwesome Grid.Column="1" Grid.Row="0" Margin="0,0,10,0" Icon="SortAmountAsc" Foreground="#919191" HorizontalAlignment="Right" VerticalAlignment="Bottom"/> 
              <StackPanel Grid.Column="0" Grid.ColumnSpan="3" Margin="0,10,0,0" Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#dbdbdb"> 
               <TextBox Style="{StaticResource Filter}" /> 
              </StackPanel> 
             </Grid> 
            </StackPanel> 
           </DataTemplate> 
          </Setter.Value> 
         </Setter> 
        </Trigger> 
        <Trigger Property="SortDirection" Value="Descending"> 
         <Setter Property="Background" Value="#F0F0F0"/> 
         <Setter Property="ContentTemplate"> 
          <Setter.Value> 
           <DataTemplate> 
            <StackPanel> 
             <Grid> 
              <Grid.RowDefinitions> 
               <RowDefinition Height="Auto"/> 
               <RowDefinition Height="Auto"/> 
              </Grid.RowDefinitions> 
              <Grid.ColumnDefinitions> 
               <ColumnDefinition Width="*"/> 
               <ColumnDefinition Width="Auto"/> 
              </Grid.ColumnDefinitions> 
              <TextBlock Grid.Row="0" Grid.Column="0" Margin="5,0,10,0" TextWrapping="WrapWithOverflow" Text="{Binding}"/> 
              <fa:FontAwesome Grid.Row="0" Grid.Column="1" Margin="0,0,10,0" Icon="SortAmountDesc" Foreground="#919191" HorizontalAlignment="Right" VerticalAlignment="Bottom"/> 
              <StackPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="1" Margin="0,10,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#dbdbdb"> 
               <TextBox Style="{StaticResource Filter}" /> 
              </StackPanel> 
             </Grid> 
            </StackPanel> 
           </DataTemplate> 
          </Setter.Value> 
         </Setter> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 

아무것도 : enter image description here

배후 관련 스타일은 다음되지 않습니다 :

는 시각적으로는 다음과 같이, 내가 3 경우에 "P"를 입력 보인다 DataGrid에 :

<DataGrid x:Name="History_DataGrid" Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3" HeadersVisibility="Column" GridLinesVisibility="None" ItemsSource="{Binding history}" AutoGenerateColumns="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" SelectionMode="Single" SelectionUnit="FullRow" CanUserAddRows="false" SelectionChanged="History_DataGrid_SelectionChanged" Sorting="History_DataGrid_Sorting"> 
     <DataGrid.Columns> 
      <DataGridTextColumn Header="{x:Static resx:Resources.Product}" Binding="{Binding Path=Product}" Width="*" ElementStyle="{StaticResource Wrap}" /> 
      <DataGridTextColumn Header="{x:Static resx:Resources.Label}" Binding="{Binding Path=Label}" Width="*" ElementStyle="{StaticResource Wrap}" /> 
      <DataGridTextColumn Header="{x:Static resx:Resources.Color}" Binding="{Binding Path=Color}" Width="*" ElementStyle="{StaticResource Wrap}" /> 
      <DataGridTextColumn Header="{x:Static resx:Resources.Volume}" Binding="{Binding Path=Volume}" Width="Auto" ElementStyle="{StaticResource Wrap}" /> 
      <DataGridTextColumn Header="{x:Static resx:Resources.LastPrinted}" Binding="{Binding Path=LastPrinted, StringFormat=\{0:yyyy-MM-dd\}}" Width="Auto" ElementStyle="{StaticResource Wrap}" /> 
      <DataGridTextColumn Header="{x:Static resx:Resources.TimesPrinted}" Binding="{Binding Path=TimesPrinted}" Width="Auto" ElementStyle="{StaticResource Wrap}" /> 
     </DataGrid.Columns> 
    </DataGrid> 

가능한 방법이 내가 아무 생각이 없지만 방법을 달성하기 위해, 문제를 우회 :

  • 정렬 후 을 변경하지 않는 필터 상자를 사용하여 두 번째 머리글 행을 만듭니다. 이벤트 사이클의 끝에서 필터 텍스트 상자의 값을 설정
  • , 나는 텍스트 상자에 액세스 할 수 있어야합니다 하지만, 내가 페이징에게 데이터 그리드가 반등 후 트리거 컨트롤을 준비하는 기능을 가지고 인스턴스가 필요합니다. 정렬되지 않은 경우 할 수 있지만 열을 정렬 할 때 상자를 찾을 수 없습니다.

이 문제를 해결하는 방법에 대한 아이디어는 실제로 큰 문제이며 검색 결과 중 아무 것도 유용하지 않습니다.

편집 : 설명

에 대한 몇 가지 시나리오가 작동해야하고 어떻게만큼 내가하지 일종처럼 작동하는 방법 :

  1. 나는 텍스트 상자에 'A'를 입력합니다.

  2. DataGrid의 원본은 텍스트 "A"에서 필터링되고 리바운드됩니다.

  3. 텍스트 상자에 "A"가 필터로 표시됩니다.

  4. 입력란에 'B'를 입력합니다.

  5. DataGrid의 원본은 텍스트 "AB"에서 필터링되고 리바운드됩니다.

  6. 텍스트 상자에 "AB"가 필터로 표시됩니다.

    1. 내가 열을 정렬하고이 텍스트 상자에 이미 텍스트를 잃는다 : 열이 정렬됩니다

    , 꽤 다르게 동작합니다.

  7. 입력란에 'A'를 입력합니다.

  8. DataGrid의 원본은 텍스트 "A"에서 필터링되고 리바운드됩니다.

  9. 텍스트 상자는 비워두고 "A"를 필터로 표시하지 않습니다.

  10. 입력란에 'B'를 입력합니다.

  11. DataGrid의 원본은 "AB"가 아니라 "B"가 아닌 "B"텍스트로 필터링되어 리바운드됩니다.

  12. 텍스트 상자가 비어 있고 필터로 "A", "B"또는 "AB"가 표시되지 않습니다.

결과적으로 필터를 제거하는 것은 매우 어렵고 TextChanged 이벤트를 삭제하고 트리거 할 텍스트가 없습니다.

답변

0

저는 회사의 WPF 전문가들과 연락을 취하고 실행 가능한 솔루션에 도달 할 수 있도록 도와주었습니다.

편집 된 스타일은 다음과 같이 보입니다 :

<Page.Resources> 
<Style x:Key="Filter" TargetType="TextBox"> 
    <EventSetter Event="TextChanged" Handler="Filter_TextBox_TextChanged"/> 
</Style> 
<Style TargetType="{x:Type DataGrid}"> 
    <!-- Some visual styling like margins and colors on the grid --> 
    <Style.Resources> 
     <Style TargetType="{x:Type DataGridColumnHeader}"> 
      <!-- Some visual styling like margins and colors on the header --> 
      <Setter Property="ContentTemplate"> 
       <Setter.Value> 
        <DataTemplate> 
         <StackPanel> 
          <Grid> 
           <Grid.RowDefinitions> 
            <RowDefinition Height="Auto"/> 
            <RowDefinition Height="Auto"/> 
           </Grid.RowDefinitions> 
           <Grid.ColumnDefinitions> 
            <ColumnDefinition Width="*"/> 
            <ColumnDefinition Width="Auto"/> 
            <ColumnDefinition Width="Auto"/> 
           </Grid.ColumnDefinitions> 
           <TextBlock Grid.Column="0" Grid.Row="0" Margin="10,0,10,0" TextWrapping="WrapWithOverflow" Text="{Binding}"/> 
            <fa:FontAwesome Grid.Column="1" Grid.Row="0" x:Name="SortIcon1" Icon="LongArrowUp" Foreground="#dbdbdb" HorizontalAlignment="Right" VerticalAlignment="Bottom"/> 
            <fa:FontAwesome Grid.Column="2" Grid.Row="0" x:Name="SortIcon2" Margin="0,0,10,0" Icon="LongArrowDown" Foreground="#dbdbdb" HorizontalAlignment="Right" VerticalAlignment="Bottom"/> 
           <StackPanel Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="1" Margin="0,10,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Background="#dbdbdb"> 
            <TextBox Style="{StaticResource Filter}" /> 
           </StackPanel> 
          </Grid> 
         </StackPanel> 
         <DataTemplate.Triggers> 
          <DataTrigger Binding="{Binding SortDirection, RelativeSource={RelativeSource AncestorType=DataGridColumnHeader}}" Value="Ascending"> 
           <Setter TargetName="SortIcon1" Property="Icon" Value="SortAmountAsc" /> 
           <Setter TargetName="SortIcon2" Property="Visibility" Value="Hidden" /> 
          </DataTrigger> 
          <DataTrigger Binding="{Binding SortDirection, RelativeSource={RelativeSource AncestorType=DataGridColumnHeader}}" Value="Descending"> 
           <Setter TargetName="SortIcon1" Property="Icon" Value="SortAmountDesc" /> 
           <Setter TargetName="SortIcon2" Property="Visibility" Value="Hidden" /> 
          </DataTrigger> 
         </DataTemplate.Triggers> 
        </DataTemplate> 
       </Setter.Value> 
      </Setter> 
      <Style.Triggers> 
       <Trigger Property="SortDirection" Value="Ascending"> 
        <Setter Property="Background" Value="#F0F0F0"/> 
       </Trigger> 
       <Trigger Property="SortDirection" Value="Descending"> 
        <Setter Property="Background" Value="#F0F0F0"/> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 

을 기본적으로 내 주요 실수가 정렬 상태 당 DataTemplate을가 하였다. 그들은 Header의 이벤트에 바인딩 된 Datatemplate에 트리거를 추가 할 수 있다고 지적했습니다. 내 정렬 아이콘에 이름을 추가하면 새 트리거 섹션에서 TargetName 특성으로 변경할 수있었습니다.

텍스트 상자의 인스턴스가 하나 밖에 없으므로 문제가 해결되었습니다.