2014-01-22 5 views
1

내가 WPF와 실버 라이트에 완전히 새로운 해요, 나는 많은 것을 배울가 ...바인딩 속성은

내가 템플릿을 포함하는 목록 상자를 가지고 만든

<ListBox ItemsSource="{Binding itemList}" x:Name="list"> 
    <ListBox.ItemTemplate> 
     <DataTemplate x:Name="datatemplate" > 
      <Grid Name="{Binding Id}"> 
       <TextBlock Text="{Binding Txt}"></TextBlock> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
내가
<ListBoxItem x:Name="lb_a" AutomationProperties.AutomationId="lb_1"> 
    <Grid Name="grid_a"> 
     <TextBlock Text="aa"></TextBlock> 
    </Grid> 
</ListBoxItem> 
<ListBoxItem x:Name="lb_b" AutomationProperties.AutomationId="lb_2"> 
    <Grid Name="grid_b"> 
     <TextBlock Text="bb"></TextBlock> 
    </Grid> 
</ListBoxItem> 
... 

내가 어떻게 할 수

에서와 같이 각 목록 항목에 AutomationProperties.AutomationId를 할당해야

? 도 가능합니까?

답변

8

당신은 ItemContainerStyle에 연결된 속성을 설정할 수 있습니다

<ListBox ItemsSource="{Binding itemList}" x:Name="list"> 
    <ListBox.ItemTemplate> 

    </ListBox.ItemTemplate> 
    <ListBox.ItemContainerStyle> 
     <Style TargetType="ListBoxItem"> 
      <Setter Property="AutomationProperties.AutomationId" Value="{Binding MyAutomationIdProperty}"/> 
     </Style> 
    </ListBox.ItemContainerStyle> 
</ListBox> 
+0

좋은를! 하지만 나는 silverlight에 x : Type이 지원되지 않습니다 ... – Doc

+1

@Doc'TargetType = "ListBoxItem"'을 시도하십시오. dkozl, 나는 이미 귀하의 답변을 수정했으며, 승인 대기 중입니다. – John

+0

@Doc, 놓친 Silverlight 태그. John이 맞습니다.'{x : Type}'비트를 제거하면 제대로 작동합니다. 업데이트 된 답변 확인 – dkozl