如何将我的按钮绑定在绑定列表框外?在绑定列表框外绑定按钮
此列表框包含我的搜索结果,每个搜索结果都有一个“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 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')"
这告诉我的是,WPF在的ObservableCollection <串寻找SearchHistory.Command_DeleteHistoryItem> SearchHistory ...不过没有关系,我不得不命令在我视图模型结合,而不是在的ObservableCollection <字符串> SearchHistory
我想有这样
的新模式,并在我的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");
}
}
}
现在的问题是,我怎么删除的项目,如果我这样做呢?
那么最好的方法是什么?
我认为你只是需要'
@ sa_ddam213>对不起,现在XAML代码已经更新了。但是,你建议不起作用 – 2013-02-15 00:12:39