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

“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?
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.