2012-01-26 18 views
6

WPFデータグリッド内の個々のセルにオートメーションIDを渡している途中ですが、少し突っ込んでしまいました。グリッド内の位置(行インデックスと列インデックス)に従ってセルの名前を付けることにしました。 UIインスペクタを使用して、問題のDataGridCellsのいずれかをハイライト表示すると、次のプロパティを示していますXAML - オートメーションIDへのセルの行と列のインデックスのバインド

GridItem.Row: 2 GridItem.Column: 0

を...私はの結合を介してこれらのプロパティにアクセスすることができると信じて私をリードしています。しかし、私はこの数日間、インターネットの使い方については、ここ数日間はもっと良いところを過ごしましたが、何も見つかりませんでした。次のように

現在のXAMLコードは(「???」がプレースホルダである)である。これらの特性のようなパスは

<DataGrid.CellStyle> 
    <Style TargetType="{x:Type DataGridCell}"> 
    <Setter Property="AutomationProperties.AutomationId"> 
     <Setter.Value> 
     <MultiBinding StringFormat="cell:{0}-{1}"> 
      <Binding ??? /> 
      <Binding ??? /> 
     </MultiBinding> 
     </Setter.Value> 
    </Setter> 
    </Style> 
</DataGrid.CellStyle> 

存在しますか?または、個々のセルに一意の自動化IDを付与する別の方法がありますか?私はWPFとXAMLにはあまり経験がありませんので、どんなポインタでも感謝しています。

ありがとうございます。

+0

私はわからないけど、してみてください<バインディングパス= "GridItem.Column" RelativeSource = { RelativeSource Mode = Self} /> – Eyjafjallajokull

+0

スニペットを挿入しようとしましたが、残念ながら正しい結果が得られませんでした(automationIdは何らかの理由で空白です)。応答していただきありがとうございます - 私は物事で遊んでいきます、そして、私が何かにつまずくと投稿します。 – CSD

答えて

6

は、それが最終的に動作するようになりました。他の人がメリットを享受できるようにここにソリューションを掲載する。

の背後にあるコードは(http://gregandora.wordpress.com/2011/01/11/wpf-4-datagrid-getting-the-row-number-into-the-rowheader/をオフに基づいて):

Private Sub DataGrid_LoadingRow(sender As System.Object, e As System.Windows.Controls.DataGridRowEventArgs) 
    e.Row.Tag = (e.Row.GetIndex()).ToString() 
End Sub 

とXAML:

<DataGrid ... LoadingRow="DataGrid_LoadingRow" > 

<DataGrid.ItemContainerStyle> 
    <Style TargetType="{x:Type DataGridRow}"> 
    <Setter Property="AutomationProperties.AutomationId"> 
     <Setter.Value> 
     <MultiBinding StringFormat="Row{0}"> 
      <Binding Path="(DataGridRow.Tag)" 
        RelativeSource="{RelativeSource Mode=Self}" /> 
     </MultiBinding> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="AutomationProperties.Name"> 
     <Setter.Value> 
     <MultiBinding StringFormat="Row{0}"> 
      <Binding Path="(DataGridRow.Tag)" 
        RelativeSource="{RelativeSource Mode=Self}" /> 
     </MultiBinding> 
     </Setter.Value> 
    </Setter> 
    </Style> 
</DataGrid.ItemContainerStyle> 

... 

<DataGrid.CellStyle> 
    <Style> 
    <Setter Property="AutomationProperties.AutomationId"> 
     <Setter.Value> 
     <MultiBinding StringFormat="cell{0}Col{1}"> 

      <!-- bind to row automation name (which contains row index) --> 
      <Binding Path="(AutomationProperties.Name)" 
        RelativeSource="{RelativeSource AncestorType=DataGridRow}" /> 

      <!-- bind to column index --> 
      <Binding Path="(DataGridCell.TabIndex)" 
        RelativeSource="{RelativeSource Mode=Self}" /> 

     </MultiBinding> 
     </Setter.Value> 
    </Setter> 
    </Style> 
</DataGrid.CellStyle> 

... 

</DataGrid> 
1

[OK]を、私は(データグリッドではなく、グリッドで、それは同じである必要がありません)、それをチェックしました、そして、それは動作します:

<AutomationProperties.AutomationId> 
    <MultiBinding StringFormat="{}{0} - {1}"> 
      <Binding Path="(Grid.Row)" RelativeSource="{RelativeSource Mode=Self}" /> 
      <Binding Path="(Grid.Column)" RelativeSource="{RelativeSource Mode=Self}" /> 
     </MultiBinding> 
</AutomationProperties.AutomationId> 
+0

グリッド内のすべてのセルのオートメーションIDとして "0-0"が取得されています。それは私が前に持っていたものと比べて大きな前進です - あなたの助けに感謝します! – CSD

+0

DataGridクラスとGridクラスの他のプロパティを調べました。すべてのセルに対して-1を受け取ったが、{Binding Path = "(DataGrid.SelectedIndex)" RelativeSource = "{RelativeSource Mode = Self}" /> – CSD

+0

プロパティ "GridItem.Row"と "GridItem.Column"はどこにありますか教えてください。私は、Snoopを使ってこの問題を調べるためにdatagridを使ってテストプロジェクトを作成しましたが、これらのプロパティは表示されません。 – Eyjafjallajokull

関連する問題