ListView中的WPF按钮无法在ViewModel中看到命令

Rob*_*ert 6 c# wpf mvvm

<StackPanel>
        <!--<Button Command="{Binding GetOddsCommand}" CommandParameter="{Binding}" />-->

    <ListView 
        ItemsSource="{Binding Links}"
        >
        <ListView.ItemTemplate>
            <DataTemplate>
                <Border>
                    <Button Command="{Binding GetOddsCommand}" CommandParameter="{Binding}">
                        <TextBlock >
                        <Hyperlink NavigateUri="http://www.onet.pl" >
                            <TextBlock Text="{Binding}" />
                        </Hyperlink>
                    </TextBlock>
                    </Button>
                </Border>
            </DataTemplate>
        </ListView.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)

我有MVVM应用程序.在viewmodel中我有GetOddsCommand:

public ICommand GetOddsCommand
{
    get
    {
        if (_getOddsCommand == null)
            _getOddsCommand = new RelayCommand(param => GetOdds());
        return _getOddsCommand;
    }
}

private void GetOdds()
{

}
Run Code Online (Sandbox Code Playgroud)

当我取消注释放置在StackPanel命令中的第一个按钮时效果很好.调试器步入get然后当我单击命令Debugger步骤进入GetOdds方法时.但它不适用于ListView中的第二个按钮.看起来像第二个按钮看不到GetOddsCommand,但我不明白为什么

谢谢

Bla*_*hma 16

放置一个按钮并在其中设置一个超链接没有多大意义......当您点击超链接时,您期望发生什么?
无论如何,以下代码将导致您的命令被调用:

<ListView ItemsSource="{Binding Links}" x:Name="ListView1">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Border>
                    <Button Command="{Binding ElementName=ListView1, Path=DataContext.GetOddsCommand}" CommandParameter="{Binding}">
                         <TextBlock Text="{Binding}" />
                    </Button>
                </Border>
            </DataTemplate>
        </ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)

请注意,使用的DataContext 不是 ListViewItem的ListView ...
您可能希望对CommandParameter执行相同类型的绑定 - 取决于您真正追求的内容.

现在,添加内部的超链接将导致问题 - 如果你单击超链接按钮没有真正点击,所以你不会得到命令,如果你点击一个没有超链接的区域一切都会好的...

如果你真的想的超级链接那里......你可以设置IsHitTestVisible周围文本块的假.

例如:

<TextBlock IsHitTestVisible="false">
    <Hyperlink NavigateUri="http://www.onet.pl"  >
    <TextBlock Text="{Binding}" />
</TextBlock>
Run Code Online (Sandbox Code Playgroud)


Uco*_*dia 9

这是因为您在不同的数据上下文中绑定命令.

在StackPanel中,您将绑定当前数据上下文中的命令,该上下文可能是包含该命令的视图模型.

在ListView中,您将命令绑定到不同的数据上下文,这是当前列表项,我认为它是一个可能不保存命令的Link对象.

如果希望命令具有与StackPanel中相同的行为,只需为列表视图指定名称,并对ListView数据上下文而不是ListViewItem数据上下文进行绑定.

<ListView x:Name="linksListView" ItemsSource="{Binding Links}"> 
    <ListView.ItemTemplate>
        <DataTemplate>
            <Border>
                <Button Command="{Binding DataContext.GetOddsCommand, ElementName=linksListView}"
                        CommandParameter="{Binding DataContext, ElementName=linksListView}">
                    <TextBlock>
                        <Hyperlink NavigateUri="http://www.onet.pl" >
                            <TextBlock Text="{Binding}" />
                        </Hyperlink>
                    </TextBlock>
                </Button>
            </Border>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>
Run Code Online (Sandbox Code Playgroud)

  • 抱歉,在绑定中忘记了 DataContext。更正。 (2认同)
  • 另一位海报稍后给出相同答案并得到验证? (2认同)
  • 这是一条救命的评论,看到它很容易,但很难找到。 (2认同)