wpf - 如何在listview中绑定(bind)按钮

标签 wpf mvvm

我无法将 listview 中的按钮绑定(bind)到 GoToTask 属性,每隔一个绑定(bind),包括网格中的第二个按钮都可以正常工作。

<Grid>
    <ListView ItemsSource="{Binding Projects}" SelectedItem="{Binding SelectedProject, Mode=TwoWay}" Grid.Row="1">
        <ListView.Resources>
            <Style TargetType="{x:Type GridViewColumnHeader}">
                <Setter Property="Visibility" Value="Collapsed"/>
            </Style>
        </ListView.Resources>
        <ListView.View>
            <GridView>
                <GridViewColumn DisplayMemberBinding="{Binding Name}"/>
                <GridViewColumn Header="Go">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Button Content="Go" Command="{Binding Path=GoToTasks, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>
    <Button Height="20" Width="50" HorizontalAlignment="Right" Command="{Binding GoToTasks}" Content="Go"/>
</Grid>

一切都在 UserControl 中。
怎么了?

最佳答案

GridViewColumn 不是 VisualTree 的一部分,因此您不能使用 FindAncestor。
Similar here .试试 ElementName,据我记得它使用 LogicalTree。

关于wpf - 如何在listview中绑定(bind)按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27453567/

相关文章:

c# - WPF 功能区控件

C# 将 viewmodel 的属性传递给调度程序

javascript - 如何将 ViewModel Store 绑定(bind)到 View?

.net - 我可以使用 C++/CLI (.NET Winforms/WPF) 为使用 native C 和 C++ 编写的应用程序提供 GUI

c# - "Two-dimensional"用户控件

wpf - BitmapSource.CopyPixels - stride 的好值是什么?

wpf - 什么 MVVM 框架适合?

wpf - 在 XAML 中设置样式的 ControlTemplate 的属性

wpf - 如何更改 oxyplot 中的默认 x 轴标题?

.net - 通过添加 View State 功能修改现有的 MVVM 基础架构