2013-06-29 75 views
0
<ListBox Name="serumListBox" VerticalContentAlignment="Stretch" 
    ScrollViewer.HorizontalScrollBarVisibility="Visible" ItemsSource="{Binding Path=SerumList}"> 
     <ListBox.Resources> 
      <Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}"> 
       <Style.Triggers> 
        <DataTrigger Binding="{Binding IsMouseOver,RelativeSource={RelativeSource Self}}" 
         Value="True"> 
         <Setter Property="IsSelected" Value="True" /> 
        </DataTrigger> 
       </Style.Triggers> 
      </Style> 
     </ListBox.Resources> 
     <ListBox.ItemsPanel> 
      <ItemsPanelTemplate> 
       <StackPanel Orientation="Horizontal"/> 
      </ItemsPanelTemplate> 
     </ListBox.ItemsPanel> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Vertical" > 
        <Button Width="250" Height="70" 
         HorizontalContentAlignment="Stretch" Click="SerumListItem_Click" > 
         <Button.ContentTemplate> 
          <DataTemplate> 
           <StackPanel> 
            <Button Click="SerumListItemRemove_Click" VerticalAlignment="Top" HorizontalAlignment="Right" Width="22" > 
             <Image Source="./Images/close.png"></Image> 
            </Button> 
            <Label VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,-9,0,0" Content="{Binding Name}" /> 
            <Label VerticalAlignment="Top" HorizontalAlignment="Left" Margin="0,-4,0,0" Content="{Binding LotNum}"/> 
           </StackPanel> 
          </DataTemplate> 
         </Button.ContentTemplate> 
        </Button> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

代碼背後:如何在列表框嵌套模板中綁定變量?

public partial class MainWindow : MetroWindow 
{ 
    private ObservableCollection<Serum> serumList = new ObservableCollection<Serum>(); 

    public ObservableCollection<Serum> SerumList 
    { 
     get { return serumList; } 
     set { serumList = value; } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 
     serumListBox.ItemsSource = SerumList; 
    } 

    private void buttonInsert_Click(object sender, RoutedEventArgs e) 
    { 
     serumList.Add(new Serum() { Name = "aadasfas", Year = "2", Month = "2", LotNum = "2", Type = "2" }); 
    } 

    private void SerumListItemRemove_Click(object sender, RoutedEventArgs e) 
    { 
     serumList.RemoveAt(serumListBox.SelectedIndex); 
    } 

    private void SerumListItem_Click(object sender, RoutedEventArgs e) 
    { 

    } 
} 

由於按鈕的DataTemplate是我無法正確地綁定標籤content.Also沒有找到這個kind.Any想法的任何資源或實例列表框DataTemplate中的嵌套模板?

回答

1

試試你的Label.ContentBinding的是:

... 
<Label Margin="0,-9,0,0" 
     HorizontalAlignment="Left" 
     VerticalAlignment="Top" 
     Content="{Binding DataContext.Name, 
          RelativeSource={RelativeSource FindAncestor, 
                 AncestorType={x:Type ListBoxItem}}}" /> 
<Label Margin="0,-4,0,0" 
     HorizontalAlignment="Left" 
     VerticalAlignment="Top" 
     Content="{Binding DataContext.LotNum, 
          RelativeSource={RelativeSource FindAncestor, 
                 AncestorType={x:Type ListBoxItem}}}" /> 
... 
+0

完善和感謝! – armin