2013-02-15 42 views
1

如何将我的按钮绑定在绑定列表框外?在绑定列表框外绑定按钮

此列表框包含我的搜索结果,每个搜索结果都有一个“X”按钮,应该删除搜索历史项目。

这里是我的列表框的预览 enter image description here
“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"); 
     } 
    } 
} 

现在的问题是,我怎么删除的项目,如果我这样做呢?


那么最好的方法是什么?

+0

我认为你只是需要'

+0

@ sa_ddam213>对不起,现在XAML代码已经更新了。但是,你建议不起作用 – 2013-02-15 00:12:39

回答

1
<Button Command="{Binding DataContext.Search.Command_DeleteHistoryItem,RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}}"/> 

您需要使用RelativeSource为了爬上可视化树,并获得了列表框本身的引用,那么你就可以得到它的DataContext,这应该是您的视图模型。

+0

工作!谢谢!现在我必须阅读有关祖先。我真的不知道如何使用它,但有一种感觉可能会有所帮助。感谢你的回答! – 2013-02-15 00:24:35

+0

现在我怎么知道哪个项目被选中? – 2013-02-15 00:25:41

+0

您可以使用CommandParameter =“{Binding}”'将选定的项目发送到您的ICommand。 – 2013-02-15 00:41:17