在绑定的ListBox之外绑定一个Button

时间:2013-02-15 00:04:59

标签: wpf xaml data-binding wpf-controls mvvm-light

如何将我的按钮绑定在绑定的ListBox之外?

此列表框包含我的搜索结果,每个搜索结果都有一个“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错误:40:BindingExpression路径错误:   'object'''String'上找不到'SearchHistory'属性   (的HashCode = -1127982548)”。   BindingExpression:路径= SearchHistory.Command_DeleteHistoryItem;   DataItem ='String'(HashCode = -1127982548);目标元素是'Button'   (名称= ''); target属性是'Command'(类型'ICommand')“

这告诉我,WPF正在 ObservableCollection&lt; string&gt;中寻找 SearchHistory.Command_DeleteHistoryItem 。 SearchHistory ..但不,我必须在 ViewModel 中绑定命令,而不是在 ObservableCollection&lt; string&gt;中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");
        }
    }
}

现在的问题是,如果我这样做,我该如何删除该项?


那么最好的方法是什么?

1 个答案:

答案 0 :(得分:1)

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

你需要使用RelativeSource来爬上Visual Tree并获得对ListBox本身的引用,然后你可以获得它的DataContext,它应该是你的ViewModel。