1

I have seen this question asked a few times but I have not seen been able to find a complete answer to my scenario.

Within my project I have a user control that I have created as a listbox item. Within this user control I have a button

            <Button x:Name="DetailButton"
                Grid.Column="1"
                Width="107"
                Height="23"
                Margin="196,94,0,0"
                HorizontalAlignment="Left"
                VerticalAlignment="Top"
                Click="MoreDetail_Click"
                Command="{Binding GetCFSDetailCommand}"
                Content="View Details [+]" />

the button has a click event specific to the view this basically expands or collapses a grid row based upon the state of visibility. I used an event here since it was specific to the ui. This button also has a command which is called in the VM.

VM code

public class SearchViewModel : INotifyPropertyChanged
{
    private DelegateCommand _getCFSDetailCommand;
public DelegateCommand GetCFSDetailCommand
    {
        get
        {
            if (this._getCFSDetailCommand == null)
                this._getCFSDetailCommand = new DelegateCommand(GetCFSDetailCommandExecute, CanGetCFSDetailCommandExecute);

            return this._getCFSDetailCommand;      
        }

    }
private void GetCFSDetailCommandExecute(object parameter)
    {
        //bind collection to model call here
    }

The issue I am having is the command on the button is "lost" or never called when inside the listbox item I have the view bound to the vm and if I place this command on any other button within the view the command is called. Can anyone help me understand how to call a Command bound to button within a listbox item?

Thank you in advance

4

2 回答 2

2

randyc,在您的原始(第一篇)帖子中,您将 绑定CommandParameter到列表项的本地数据上下文。在第二篇文章中,您错过了该绑定,我认为在第二个端口的上下文中这是不可能的。在您的情况下,我建议使用Element to Element 绑定来绑定到GetCFSDetailCommand来自父数据上下文的命令。

于 2011-06-15T07:25:21.597 回答
1

将用户控件中的命令作为列表框项调用的问题是模式正在控件的上下文中查找命令。显然,列表框项跳到了可视化树之外,因此绑定不会被继承。
为了纠正这个问题,我需要将按钮的数据上下文显式设置为 ViewModel。这最终通过使用元素到元素绑定来解决,这使我可以将用户控件的数据上下文指向包含它的主视图。

希望这可以帮助

于 2011-06-01T15:15:31.983 回答