如何在绑定的 ListBox 之外绑定我的按钮?
这个列表框包含我的搜索结果,每个搜索结果都有一个“X”按钮,应该删除搜索历史项目。
这是我的列表框“X”按钮的预览,
仅当我将鼠标悬停在某个项目上时才会出现
这是我的 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>
这是我的 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");
}
}
问题有这个错误
“System.Windows.Data 错误:40:BindingExpression 路径错误:在 'object' ''String' (HashCode=-1127982548) 上找不到 'SearchHistory' 属性。BindingExpression:Path=SearchHistory.Command_DeleteHistoryItem; DataItem='String' ( HashCode=-1127982548); 目标元素是 'Button' (Name=''); 目标属性是 'Command' (类型 'ICommand')"
这告诉我,WPF 正在寻找SearchHistory.Command_DeleteHistoryItem在ObservableCollection<string> SearchHistory .. 但是不,我必须在我的ViewModel中绑定命令,而不是在ObservableCollection<string> SearchHistory
我在想有一个像这样的新模型
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()
{
}
}
在我的 SearchHistory ObservableCollection 中像这样
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");
}
}
}
现在的问题是,如果我这样做了,我该如何删除该项目?
那么最好的方法是什么?