1

What I'm trying to do is a combobox that have the favorite values on top, with a different background color and button. Right now I have:

<UserControl x:Class="ComboBoxWithButton"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d"
             Name="root"
             d:DesignWidth="300" Height="25">

    <ComboBox 
         x:Name="ComboBoxBtn" 
         VerticalAlignment="Top" 
         HorizontalAlignment="Left" 
         Margin="0,0,0,-1" 
         Width="300" 
         ItemsSource="{Binding Source, RelativeSource={RelativeSource AncestorType=UserControl}}"
         SelectedItem="{Binding Path=Selected, Mode=TwoWay, RelativeSource={RelativeSource AncestorType=UserControl}}">
       <ComboBox.Resources>
            <Style TargetType="ComboBoxItem">
                ????
            </Style>
        </ComboBox.Resources>
        <ComboBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.Style>
                        <Style TargetType="Grid">
                            <Setter Property="Background" Value="#FFE6E6FA"/>
                        </Style>
                    </Grid.Style>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Label Content="{Binding}" Width="250" />
                    <Button Grid.Column="1" Command="{Binding CommandButton, ElementName=root}"
                            CommandParameter="{Binding}">+</Button>
                </Grid>
            </DataTemplate>

        </ComboBox.ItemTemplate>
    </ComboBox>
</UserControl>

Right now I have an Add button so I can add my items as favorites. But what I want now is, based on the item I represent it as favorite or not.

Case is a favorite have a different background color an a [-] button (to remove). Case is not the background is white as usual and have a [+].

9
  • If you want to add buttons you will need to modify the ComboBox control template, which is here, msdn.microsoft.com/en-us/library/dd334408%28v=vs.95%29.aspx Commented Oct 4, 2016 at 14:46
  • My main problem is not the button. Is how I can "say" if the item is a favorite (with the help of a method that checks if he favorite or not) then do this, otherwise do that. Commented Oct 4, 2016 at 14:47
  • You could have a Grid with the two controls. One control has a binding Visibility = IsFavorite and the other Visibility = IsNotFavorite Commented Oct 4, 2016 at 14:51
  • You need another ItemsControl which have your "Favorites" as an ItemsSource. This is as @ÞÄTRÏÇK mentioned in the ComboBox control template not in the ItemTemplate Commented Oct 4, 2016 at 14:52
  • And I do that in the same XAML file? But with 2 different ControlTemplate? Can you just give me a small example please? Commented Oct 4, 2016 at 14:54

1 Answer 1

1

See if a ContentControl helps you here in place of where you are using a Button.

 <ComboBox.ItemTemplate>
 <DataTemplate>
 <ContentControl>
    <Style TargetType="ContentControl">
        <Style.Triggers>
            <DataTrigger Binding="{Binding ...}" Value="True">
                <Setter Property="Content">
                    <Setter.Value>
                            <Grid>
                                <Grid.Style>
                                    <Style TargetType="Grid">
                                        <Setter Property="Background" Value="#FFE6E6FA"/>
                                    </Style>
                                </Grid.Style>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Label Content="{Binding}" Width="250" />
                                <Button Grid.Column="1" Command="{Binding CommandButton, ElementName=root}"
                        CommandParameter="{Binding}">+</Button>
                            </Grid>
                        </Setter.Value>
                </Setter>
            </DataTrigger>
            <DataTrigger Binding="{Binding ...}" Value="False">
                <Setter Property="Content">
                    <Setter.Value>
                            <Grid>
                                <Grid.Style>
                                    <Style TargetType="Grid">
                                        <Setter Property="Background" Value="Yellow"/>
                                    </Style>
                                </Grid.Style>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Label Content="{Binding}" Width="250" />
                                <Button Grid.Column="1" Command="{Binding CommandButton, ElementName=root}"
                                    CommandParameter="{Binding}">-</Button>
                            </Grid>
                        </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</ContentControl>
</DataTemplate>
</ComboBox.ItemTemplate>
Sign up to request clarification or add additional context in comments.

5 Comments

I guess it help regarding the button part and about the Background? What shoud I put on the bidding?
@AndreRoque u can use ContentControl as DataTemplate itself and change Button part urself.
Thanks a lot! And if I understand correctly then I bind the 2 different DataContext right?
@AndreRoque 2 diff DC ? where buttons ?
No one can stop you from binding whatever u like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.