0

I want to add button in combobox, which contains ItemTemplate. First, that I tried was this:

<ComboBox Name="oilWells_comboBox"
          Style="{StaticResource MMComboBox}"
          MaxWidth="100"
          ItemsSource="{Binding DataContext.OilWellCollection, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:MainWindow}}"
          Margin="0">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                <CheckBox IsChecked="{Binding Path=IsDisplay}" Checked="FilterDataGrid" Unchecked="FilterDataGrid">
                    <CheckBox.Content>
                        <TextBlock MinWidth="100" Text="{Binding Path=Name}" HorizontalAlignment="Center" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
                    </CheckBox.Content>
                </CheckBox>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
    <Button Content="Clear" Height="20" HorizontalAlignment="Stretch"></Button>
</ComboBox>

But I've got an exception, which said what i cant add items to control, which has ItemTemplate. The second one was this:

<ComboBox Name="oilWells_comboBox"
          Style="{StaticResource MMComboBox}"
          MaxWidth="100"
          ItemsSource="{Binding DataContext.OilWellCollection, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:MainWindow}}"
          Margin="0">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                <CheckBox IsChecked="{Binding Path=IsDisplay}" Checked="FilterDataGrid" Unchecked="FilterDataGrid">
                    <CheckBox.Content>
                        <TextBlock MinWidth="100" Text="{Binding Path=Name}" HorizontalAlignment="Center" TextWrapping="Wrap" TextTrimming="CharacterEllipsis"/>
                        <Button Content="Clear" Height="20" HorizontalAlignment="Stretch"></Button>
                    </CheckBox.Content>
                </CheckBox>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

But in this case button adds after each checkbox. Have you any ideas, how to do this only once? Thanks you in advance)

ViVi
  • 4,339
  • 8
  • 29
  • 52
  • edit `Template` property of ComboBox itself. In VS right click on CB, select `Edit Template` - `Edit copy` and then add Button in that template – ASh Jul 25 '16 at 06:39
  • Your question isn't clear - Do you want the button as one of the options in the ComboBox? Do you want to create your own ComboBox which contains a button? Do you want each option in the ComboBox to contain a button? – MichaelThePotato Jul 25 '16 at 06:50
  • I want to add a button as the first combobox item, and the all next items must be items from ItemsTemplate – Дмитрий Репин Jul 25 '16 at 07:00

2 Answers2

1
  1. Right Click ComboBox in designer surface / or in Document Outline at the left side outside designer surface > Edit Template > Edit copy.

  2. This will generate some Styles under Window.Resources, find ItemsPresenter and wrap it with StackPanel and Button as shown below :

    <StackPanel Grid.ColumnSpan="2">
       <Button Content="Additional"/>
       <ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
    </StackPanel>
    
AnjumSKhan
  • 9,647
  • 1
  • 26
  • 38
0

Try this in windows loaded event and it should work.

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    Button b = new Button();
    b.Content = "My Button";
    b.Click += new RoutedEventHandler(MyBtn_Click);
    oilWells_comboBox.Items.Add(b);
}
ViVi
  • 4,339
  • 8
  • 29
  • 52