Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9295073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:31:07+00:00 2026-06-18T21:31:07+00:00

How do I bind my button outside my binded ListBox? This Listbox contains my

  • 0

How do I bind my button outside my binded ListBox?

This Listbox contains my search results and each search results has an “X” button that should delete the search history item.

Here’s a preview of my listbox
enter image description here
“X” Button only appears when I hover to an item

Here’s my XAML

<ListBox x:Name="listHistory" ItemsSource={Binding SearchHistory.SearchHistory} BorderThickness="0" Margin="0" Padding="0" HorizontalContentAlignment="Stretch">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <!-- this binds to a string in ObservableCollection<string> -->
                <TextBlock Text="{Binding }" />

                <!-- this should bind to SearchHistory.Command_DeleteHistoryItem -->
                <!-- currently, this Command="{Binding SearchHistory.Command_DeleteHistoryItem}" doesn't work
                     System.Windows.Data Error: 40 : BindingExpression path error: 'SearchHistory' 
                     property not found on 'object' ''String' (HashCode=-1127982548)'. 
                     BindingExpression:Path=SearchHistory.Command_DeleteHistoryItem; 
                     DataItem='String' HashCode=-1127982548); 
                     target element is 'Button' (Name=''); 
                     target property is 'Command' (type 'ICommand')
                -->
                <Button Command="{Binding SearchHistory.Command_DeleteHistoryItem}" Grid.Column="1" HorizontalAlignment="Right" x:Name="btnDeleteHistoryItem" Content="r" FontFamily="Marlett" Style="{DynamicResource ButtonStyle}" Visibility="Hidden" Opacity="0.75" />
            </Grid>

            <DataTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Visibility" TargetName="btnDeleteHistoryItem" Value="Visible" />
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

and here’s my ViewModel

public class ViewModel_SearchHistory : ViewModelBase
{
    ObservableCollection<string> _SearchHistory = new ObservableCollection<string>();
    public ObservableCollection<string> SearchHistory
    {
        get { return this._SearchHistory; }
        set
        {
            if (this._SearchHistory != value)
            {
                this._SearchHistory = value;
                base.RaisePropertyChanged("SearchHistory");
            }
        }
    }

    public ViewModel_SearchHistory()
    {
        this.Command_DeleteHistoryItem = new RelayCommand(DeleteHistoryItem);
    }

    public ICommand Command_DeleteHistoryItem
    {
        get;
        internal set;
    }

    public void DeleteHistoryItem()
    {
        Debug.WriteLine("blah");
    }
}

Problem there is this error

“System.Windows.Data Error: 40 : BindingExpression path error:
‘SearchHistory’ property not found on ‘object’ ”String’
(HashCode=-1127982548)’.
BindingExpression:Path=SearchHistory.Command_DeleteHistoryItem;
DataItem=’String’ (HashCode=-1127982548); target element is ‘Button’
(Name=”); target property is ‘Command’ (type ‘ICommand’)”

This tells me that, WPF is looking for SearchHistory.Command_DeleteHistoryItem in ObservableCollection<string> SearchHistory.. But NO, I have to bind the command in my ViewModel, and not in ObservableCollection<string> SearchHistory

I was thinking to have a new Model like this

public class Model_HistoryItemDetail
{
    public string Item { get; set; }
    public ICommand Delete { get; internal set; }

    public Model_HistoryItemDetail()
    {
        this.Delete = new RelayCommand(DeleteItem);
    }

    public void DeleteItem()
    {
    }
}

and in my SearchHistory ObservableCollection like this

ObservableCollection<Model_HistoryItemDetail> _SearchHistory = new ObservableCollection<Model_HistoryItemDetail>();
public ObservableCollection<Model_HistoryItemDetail> SearchHistory
{
    get { return this._SearchHistory; }
    set
    {
        if (this._SearchHistory != value)
        {
            this._SearchHistory = value;
            base.RaisePropertyChanged("SearchHistory");
        }
    }
}

The problem now is, how do I delete the item if I did that way?


So what’s the best way to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T21:31:08+00:00Added an answer on June 18, 2026 at 9:31 pm
    <Button Command="{Binding DataContext.Search.Command_DeleteHistoryItem,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}"/>
    

    You need to use RelativeSource in order to climb up the Visual Tree and get a reference to the ListBox itself, then you can get its DataContext, which should be your ViewModel.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing this: return this.each(function(){ $.this.load('searchInterface.html',function(){ console.log('load'); //a lot of code }); $('#more_button').bind('click',more()); function
Every example I've seen on this subject has shown a Button being bound to
I have some userControl that contain simple button. I want to bind the button
I am trying to handling datafrom's internal Add/Delete/Edit command form button control outside datafrom.
Check this example: $('button').bind('click focus', function() { // Did I click or focus? });
I have this button : self.mybutton= wx.Button(self, -1, label= mylabel, pos=(100,180)) self.Bind(wx.EVT_BUTTON, self.Onbutton, self.mybutton)
I have this <a href=A.html data-ajax=false class=btn>A</a> <a href=B.html data-ajax=false>B</a> <script> $('a, button').bind('touchstart', function(e)
I was told previously that I should not bind events inside of a function.
function LolClass(){ this.init = function(){ button_a.bind(tap, function(){ this.refreshFields(); // doesn't work //refreshFields(); // doesn't
Is it possible to bind the current item to a tag in button from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.