ListView 中的 WPF 按钮在 ViewModel 中看不到命令 [英] WPF button in ListView can not see command in ViewModel

查看:23
本文介绍了ListView 中的 WPF 按钮在 ViewModel 中看不到命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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 应用程序.在视图模型中,我有 GetOddsCommand:

I have MVVM application. In viewmodel I have GetOddsCommand:

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

private void GetOdds()
{

}

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

When I uncomment first button placed in StackPanel command works good. Debugger step into get and then when I click command Debugger step into GetOdds method. But it doesn't work in second button which is in the ListView. Looks like second button cannot see GetOddsCommand, but I don't understand why

谢谢

推荐答案

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

Putting a button and inside it a Hyperlink doesn't make much sense... What do you expect to happen when you click on the hyperlink?
Anyways, the following code will cause your command to be called:

<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 进行相同类型的绑定 - 取决于您真正想要的是什么.

Notice the DataContext used is that of the ListView not of the ListViewItem...
You might want to do the same kind of binding for the CommandParameter - Depends on what you're really after.

现在,在里面添加超链接会导致问题——如果你点击超链接,按钮并没有真正被点击,所以你不会得到命令,如果你点击一个没有超链接的区域,一切都会好起来的..

Now, adding the hyperlink inside will cause problems - if you click on the Hyperlink the button isn't really clicked so you won't get the command, if you click on an area without the hyperlink everything will be fine...

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

If you really do want the hyperlink there... You can set the IsHitTestVisible of the surrounding textblock to false.

例如:

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

这篇关于ListView 中的 WPF 按钮在 ViewModel 中看不到命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆