1

I'm trying to create a custom combobox that have a list of items, and each item have an add(+) button that is suppose to add that item to a "favorite" list:

XAML:

<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" 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.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>
                    <Label Content="{Binding}" Width="250" />
                    <Button Grid.Column="1" Command="{Binding CommandButton}"
                            CommandParameter="{Binding Path=Selected}">+</Button>
                </Grid>
            </DataTemplate>
        </ComboBox.ItemTemplate>
    </ComboBox>
</UserControl>

XAML.CS:

public IEnumerable Source
        {
            get { return (IEnumerable)GetValue(SourceProperty); }
            set { SetValue(SourceProperty, value); }
        }



    public static readonly DependencyProperty SourceProperty =
        DependencyProperty.Register("Source", typeof(IEnumerable), typeof(ComboBoxWithButton), new PropertyMetadata(null));


public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register("CommandButton", typeof(ICommand), typeof(ComboBoxWithButton), new PropertyMetadata(null));

        public ICommand CommandButton
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

Then on my main view, using the combobox I have:

<controls:ComboBoxWithButton Source="{Binding AvailableClients}" Selected="{Binding SelectedClient, Mode=TwoWay}"
                                                       LostFocus="OnClientSelected"
                                                         CommandButton="{Binding AddFavoriteCommand}"/>

And:

AddFavoriteCommand = new RelayCommand<object>(AddToFavorite, f => true);

But it's not triggering my function "AddToFavorite"

4
  • That was something that I saw in a response here. But maybe is not correct. Source is a dependecy proprety yes Commented Oct 3, 2016 at 10:05
  • Added the lines that you asked. Just let me say that I guess the source is not the problem because the items are listed correctly. The problem is when I hit the button doesn't trigger the Comand/Action Commented Oct 3, 2016 at 10:14
  • no problem. thanks ;) Commented Oct 3, 2016 at 10:16
  • I edited and deleted that part, as I said is something that I saw in another case and it was a test (that didn't work) Commented Oct 3, 2016 at 10:19

1 Answer 1

1

The button is inside a DataTemplate, so each button's DataContext is different than the UserControl's DataContext.

You need to change the Command binding to access the UserControl's DataContext:

    <DataTemplate>
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>
            <Label Content="{Binding}" Width="250" />
            <Button Grid.Column="1" Command="{Binding CommandButton, RelativeSource={RelativeSource AncestorType=UserControl}}"
                    CommandParameter="{Binding Path=Selected}">+</Button>
        </Grid>
    </DataTemplate>
Sign up to request clarification or add additional context in comments.

Comments

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.