2015-10-13 28 views
5

WPFの新機能で、WinFormsにかなり快適です(これはおそらくトランジションを粗くしています)。私は古いWinFormsプロジェクトからWPFにいくつかの機能を移植体験しようとしています。WPF Datagridサイクルスルー/特定のプロパティを持つセルの選択

目的は、TextBoxの文字列に一致するDataGridのセル値を見つけることです。私はgreat exampleが正確にそれを行うバインディングを使用して見つけました。基本的に、リンクされたコードは、一致するDataGridCellの背景色をオレンジ色に変更します。バージョンを少し変更しましたが、機能は同じでなければなりません。コード例のリンクを参照してください、少し余裕がここにそれを提供するようです。私のDataGridのデータは、DataTableのものです(重要な場合)。

私がしたいことは、これらの各セル(背景色またはカスタムプロパティDataGridTextSearch.IsTextMatchを使用して決定)を選択して選択する「次へ」ボタンがあることです。提供されたコードを変更するだけのようだが、どこから始めるべきか分からない。私の古いWinFormsプロジェクトでは、リストにDataGridViewCellを格納し(Linqクエリでそれらを見つけた後)、ボタンの動作を追加してリストをインクリメントし、現在のセルを設定するだけでした。おそらく、バインディングを含むよりスマートで良い方法があると思われます。また、これらの一致するセルをオプションに追加する方法もわかりません。要約すると、(BackgroundまたはカスタムのDataGridTextSearch.IsTextMatchプロパティに基づいて)特定のDataGridCellを循環し、それらを選択するボタンが必要です。

ありがとうございます。

答えて

5

ご質問にお答えしたlinkに基づき、私はこれを解決する方法を見つけました。私の解決策ではDataGridCellTextBoxの文字列と一致すると、Tagのプロパティが "1"に設定され、Buttonがクリックされると、すべてDataGridCellsを反復してnull以外の項目が見つかるTags、最後にハイライトされたセル1つずつフォーカスされます。

XAML:

<Window Name="UI"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="Auto"/> 
     </Grid.RowDefinitions> 
     <StackPanel DataContext="{Binding ElementName=UI}" Grid.Row="0"> 
      <TextBox Name="SearchBox" TextChanged="SearchBox_TextChanged"/> 
      <DataGrid x:Name="grid" 
        m:DataGridTextSearch.SearchValue="{Binding ElementName=SearchBox, Path=Text, UpdateSourceTrigger=PropertyChanged}" 
        ItemsSource="{Binding TestData}" 
        SelectionUnit="Cell"> 
       <DataGrid.Resources> 
        <m:SearchValueConverter x:Key="SearchValueConverter" /> 
        <Style TargetType="{x:Type DataGridCell}"> 
         <Setter Property="m:DataGridTextSearch.IsTextMatch"> 
          <Setter.Value> 
           <MultiBinding Converter="{StaticResource SearchValueConverter}"> 
            <Binding RelativeSource="{RelativeSource Self}" Path="Content.Text" /> 
            <Binding RelativeSource="{RelativeSource Self}" Path="(m:DataGridTextSearch.SearchValue)" /> 
           </MultiBinding> 
          </Setter.Value> 
         </Setter> 
         <Style.Triggers> 
          <Trigger Property="m:DataGridTextSearch.IsTextMatch" Value="True"> 
           <Setter Property="Background" Value="Orange" /> 
           <Setter Property="Tag" Value="1" /> 
          </Trigger> 
         </Style.Triggers> 
        </Style> 
       </DataGrid.Resources> 
      </DataGrid> 
     </StackPanel> 
     <Button Grid.Row="1" Click="Button_Click" Content="GoNext"/> 
    </Grid> 
</Window> 

MainWindow.cs:

int currentIndex = 0; 

private void SearchBox_TextChanged(object sender, TextChangedEventArgs e) 
{ 
    currentIndex = 0; 
} 

private void Button_Click(object sender, RoutedEventArgs e) 
{ 
    var selectedCells = GetHighLightedCells(); 
    if (selectedCells.Count == 0) 
     return; 

    selectedCells[currentIndex].Focus(); 

    if (currentIndex == selectedCells.Count - 1) 
     currentIndex = 0; 
    else 
     currentIndex++; 
} 

は、メソッドのセルを強調表示するには:ここで

はあなたのアイデアを与えるために実施例であります

public List<DataGridCell> GetHighLightedCells() 
{ 
    List<DataGridCell> selectedCells = new List<DataGridCell>(); 
    foreach (DataGridRow rowContainer in GetDataGridRows()) 
    { 
     if (rowContainer != null) 
     { 
      DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer); 
      foreach (var col in grid.Columns) 
      { 
       DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(col.DisplayIndex); 
       if (cell == null) 
       { 
        grid.ScrollIntoView(rowContainer, col); 
        cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(col.DisplayIndex); 
       } 
       if (cell.Tag != null) 
       { 
        selectedCells.Add(cell); 
       } 
      } 
     } 
    } 
    return selectedCells; 
} 
public IEnumerable<DataGridRow> GetDataGridRows() 
{ 
    var itemsSource = grid.ItemsSource as IEnumerable; 
    if (null == itemsSource) yield return null; 
    foreach (var item in itemsSource) 
    { 
     var row = grid.ItemContainerGenerator.ContainerFromItem(item) as DataGridRow; 
     if (null != row) yield return row; 
    } 
} 

public static T GetVisualChild<T>(Visual parent) where T : Visual 
{ 
    T child = default(T); 
    int numVisuals = VisualTreeHelper.GetChildrenCount(parent); 
    for (int i = 0; i < numVisuals; i++) 
    { 
     Visual v = (Visual)VisualTreeHelper.GetChild(parent, i); 
     child = v as T; 
     if (child == null) 
     { 
      child = GetVisualChild<T>(v); 
     } 
     if (child != null) 
     { 
      break; 
     } 
    } 
    return child; 
} 
+1

素晴らしい。私は実際に同様の行に沿って何かをしていましたが、それは私を永遠に解決してくれました。感謝します!ありがとうございました。 – Finch042

+0

あなたは大歓迎です:) –

関連する問題