2010-05-04 3 views
1

내가 구입 한 항목과 NHibernate에에 사용하여 엔티티 클래스를 저장하는 테이블이 있습니다. 레코드는 내림차순으로 SortSale 열별로 정렬되었습니다. 무엇 IList <PurchasedItem> 개체를 기반으로 WrapPanel 버튼을 만드는 가장 좋은 방법은 무엇입니까? 각 단추에 대한 이벤트 처리기를 할당하고 싶습니다. 버튼에는 제품 이름이 표시된 제목이 표시됩니다.동적으로 생성 WrapPanel 버튼

답변

4

WrapPanel을 ItemsPanel로 사용하여 목록 상자를 만들어야합니다. XAML에서 다음과 같은 작업을 수행 할 수 있습니다

<ListBox Name="MyList" ItemsSource={StaticResource YourList}> 
    <ListBox.ItemsPanel> 
     <ItemsPanelTemplate> 
      <WrapPanel/> 
     </ItemsPanelTemplate> 
    </ListBox.ItemsPanel> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Button Height="40" MinWidth="40" Content="{Binding Id}" Click="Button_Click"/> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

을이 예제에서는 YourList이 PurchasedItem의 목록입니다 가정합니다. ItemsSource는 코드 (예 : MyList.ItemsSource = YourList;)로 설정할 수도 있습니다. 당신은 아마 할 것입니다, 그래서 나는 PurchasedItem의 ID로 버튼의 내용을 설정

private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     MessageBox.Show(((sender as Button).DataContext as PurchasedItem).Product.Name); 
    } 

참고 : 버튼을 클릭하면, 그것은 메시지 박스는 당신이 필요로하는 무엇이든 들어 표시 할 수있는 다음 전화 할게 그것을 바꾸는 것.