相关疑难解决方法(0)

"{Binding Path =.}"和"{Binding}"是否真的相等

在我的WPF项目中,我有一个ListBox来显示List<string>集合中的项目.我想让这些项目的文本可编辑,所以我将每个项目包装在带有TextBox的ItemTemplate中(可能不是最好的方法,但我是WPF的新手).我只是将TextBoxes的Text属性绑定到每个项的值时遇到了麻烦.我最后偶然发现了一个使用单个点或句点作为其Path属性({Binding Path=.})的示例:

<ListBox ItemsSource="{Binding ElementName=recipesListbox,Path=SelectedItem.Steps}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Path=.}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)

但是我不明白为什么单纯使用{Binding}不起作用.

根据微软的说法,它提出了" 双向绑定需要Path或XPath "的例外情况:

[...]句点(.)路径可用于绑定到当前源.例如,Text ="{Binding}"等同于Text ="{Binding Path =.}"

有人可以阐明这种模棱两可的行为吗?

编辑:此外,它似乎{Binding Path=.}不一定提供双向绑定,因为修改文本和移动焦点不会更新底层源(相同的源也具有在DataGrid控件上显示和成功修改的属性).我肯定在这里遗漏了一些东西.

wpf xaml binding path

46
推荐指数
4
解决办法
3万
查看次数

如何在WPF中从C#获取HyperLink文本?

我有一个WPF Hyperlink,我正试图从中获取文本内容.

例如:

<Hyperlink Command="{Binding CustomersCommand}" Name="HLCustomers">
    Customers
</Hyperlink>
Run Code Online (Sandbox Code Playgroud)

使用通常的方式访问Text属性或使用VisualTreeHelper获取某些子文本元素Hyperlink是不可能的,因为它不是可视元素.我试图从中获取文本,FirstInline但这也没有给我文本.

如何Hyperlink在运行时从上面示例中的元素获取值"Customers" ?

c# wpf

4
推荐指数
1
解决办法
4818
查看次数

标签 统计

wpf ×2

binding ×1

c# ×1

path ×1

xaml ×1