2017-06-15 112 views
1

定義的組合框添加一個按鈕,我有這個ItemsControl在我的XAML定義WPF:在DataTemplate中

<ItemsControl Grid.ColumnSpan="3" Grid.Row="1" HorizontalAlignment="Center" ItemsSource="{Binding MyItems, Mode=TwoWay}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="200"/> 
        <ColumnDefinition Width="200"/> 
        <ColumnDefinition Width="200"/> 
       </Grid.ColumnDefinitions> 
       <Label HorizontalContentAlignment="Center" Grid.Column="0" Content="{Binding FirstProperty}"/> 
       <Label HorizontalContentAlignment="Center" Grid.Column="1" Content="{Binding SecondProperty}"/> 
       <Label HorizontalContentAlignment="Center" Grid.Column="2" Content="{Binding ThirdProperty}"/> 
       <ComboBox HorizontalAlignment="Center" ItemsSource="{Binding CBSource}" Grid.Column="2" Width="140" Visibility="{Binding HasCombobox, Converter={StaticResource BoolToVis}}"> 
        <ComboBoxItem> 
         <Button Content="INeedAButtonHere"></Button> 
        </ComboBoxItem> 
       </ComboBox> 
      </Grid> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

我需要添加到這樣一個Button創建的每個ComboBox,可能是放置在的底部由於ComboBox有源碼,因此下拉菜單。顯示UserControl時,我當前的代碼會引發異常(XamlParseError)。如何做到這一點?

+0

你想在組合框的每個項目按鈕?或者只是每個組合框的按鈕? – mm8

+0

只需爲每個組合框設置一個按鈕,放置在下拉框的底部 –

回答

1

你可以使用一個CompositeCollection

<ItemsControl Grid.ColumnSpan="3" Grid.Row="1" HorizontalAlignment="Center" 
       ItemsSource="{Binding MyItems}"> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <Grid.Resources> 
        <CollectionViewSource x:Key="cvs" Source="{Binding CBSource}" /> 
       </Grid.Resources> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="200"/> 
        <ColumnDefinition Width="200"/> 
        <ColumnDefinition Width="200"/> 
       </Grid.ColumnDefinitions> 
       <Label HorizontalContentAlignment="Center" Grid.Column="0" Content="TEST"/> 
       <Label HorizontalContentAlignment="Center" Grid.Column="1" Content="{Binding SecondProperty}"/> 
       <Label HorizontalContentAlignment="Center" Grid.Column="2" Content="{Binding ThirdProperty}"/> 
      <ComboBox HorizontalAlignment="Center" Grid.Column="2" Width="140" Visibility="{Binding HasCombobox, Converter={StaticResource BoolToVis}}"> 
        <ComboBox.ItemsSource> 
         <CompositeCollection> 
          <CollectionContainer Collection="{Binding Source={StaticResource cvs}}" /> 
          <ComboBoxItem> 
           <Button Content="INeedAButtonHere"></Button> 
          </ComboBoxItem> 
         </CompositeCollection> 
        </ComboBox.ItemsSource> 
       </ComboBox> 
      </Grid> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 
+1

我甚至不知道這種工具的存在。它完美的工作,謝謝 –