wpf 绑定 ListViewItem

162 阅读1分钟

wpf 绑定 ListViewItem

Wpf 绑定ListViewItem 中的控件 CommandViewModel

1. 给Window 定义名字:

<Window x:Name="ThisWnd"> 在绑定时找到指定名字

<ListView>

    <ListView.ItemTemplate>

        <DataTemplate>

            <Button Command="{Binding ElementName=ThisWnd, Path=DataContext.CallCommand}"

            Text={Binding Name}></Button>

2. 相对路径查找

另一种方式是根据类型或层次查找

<ListView>

    <ListView.ItemTemplate>

        <DataTemplate>

            <Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}, Path=DataContext.CallCommand}"

            Text={Binding Name}></Button>

文章写于 2020-08-04, 于2023-03-21 迁移到掘金。