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

问题描述 投票:6回答:2
<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>

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

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

private void GetOdds()
{

}

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

谢谢

c# wpf mvvm
2个回答
15
投票

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

<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>

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

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

如果你真的想要那里的超链接...你可以将周围文本块的IsHitTestVisible设置为false。

e.f.:

<TextBlock IsHitTestVisible="false">
    <Hyperlink NavigateUri="http://www.onet.pl"  >
    <TextBlock Text="{Binding}" />
</TextBlock>

8
投票

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

在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>
© www.soinside.com 2019 - 2024. All rights reserved.