【WPF】绑定报错:双向绑定需要 Path 或 XPath

背景

最开始使用的是 TextBlock:

xml 复制代码
<ItemsControl ItemsSource="{Binding CameraList}">
     <ItemsControl.ItemsPanel>
         <ItemsPanelTemplate>
             <StackPanel Orientation="Horizontal"/>
         </ItemsPanelTemplate>
     </ItemsControl.ItemsPanel>
     <ItemsControl.ItemTemplate>
         <DataTemplate>
             <GroupBox>
                 <TextBlock Text="{Binding}" Foreground="Green"/> 
             </GroupBox>
         </DataTemplate>
     </ItemsControl.ItemTemplate>
 </ItemsControl>  

后面将,TextBlock改为TextBox后报错

xml 复制代码
<ItemsControl ItemsSource="{Binding CameraList}">
     <ItemsControl.ItemsPanel>
         <ItemsPanelTemplate>
             <StackPanel Orientation="Horizontal"/>
         </ItemsPanelTemplate>
     </ItemsControl.ItemsPanel>
     <ItemsControl.ItemTemplate>
         <DataTemplate>
             <GroupBox>
                 <TextBox Text="{Binding}" Foreground="Green"/>
             </GroupBox>
         </DataTemplate>
     </ItemsControl.ItemTemplate>
 </ItemsControl>

报错信息如下

首先看如何解决这个报错问题。

方案 1:使用 {Binding .}

因为 CameraList 里的每个元素就是 string,直接使用 {Binding .}TextBox 绑定当前项:

xml 复制代码
<ItemsControl ItemsSource="{Binding CameraList}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <GroupBox>
                <TextBox Text="{Binding .}" Foreground="Green"/>
            </GroupBox>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

方案 2:绑定 Path 指定 Text

虽然 Binding . 已经可以解决问题,但你也可以显式指定 Path

xml 复制代码
<TextBox Text="{Binding Path=.}" Foreground="Green"/>

Path 在绑定 List<string> 时通常可以省略。

这样就能正确绑定 List<string> 里的每个字符串到 TextBox 里。🚀且不会报错!

为啥用TextBlock这样写不报错?

TextBox 绑定报错,而 TextBlock 没有报错,主要是因为 TextBox.Text 默认是双向绑定,而 TextBlock.Text 是单向绑定

区别分析

  1. TextBox.Text="{Binding}"(默认是 Mode=TwoWay

    • TextBoxText 绑定默认是双向绑定 (Mode=TwoWay)。
    • 但是你的 CameraListList<string>,其中的 string不可修改的值类型 ,WPF 不能将 TextBox.Text 的值回写到 string,所以绑定失败。
  2. TextBlock.Text="{Binding}"(默认是 Mode=OneWay

    • TextBlock 只需要读取 string 值,不需要回写,因此不会有绑定问题。

解决方案

使用 TextBox,可以将 Binding 设置为 Mode=OneWay

xml 复制代码
<TextBox Text="{Binding Mode=OneWay}" Foreground="Green"/>

或者使用 {Binding .}

xml 复制代码
<TextBox Text="{Binding .}" Foreground="Green"/>

这样 TextBox 只读 string,不会尝试写回,就不会报错了。

总结

  • TextBlock.Text 只读,不回写,所以绑定 string 没问题。
  • TextBox.Text 需要写回,string 不是 DependencyObject,无法写回,所以报错。
  • 解决方案:使用 Mode=OneWay 或者 {Binding .}TextBox 变成单向绑定。🚀
相关推荐
绿龙术士1 天前
构建现代化WPF应用:数据驱动开发与高级特性解析
c#·wpf
wangnaisheng3 天前
【WPF】Opacity 属性的使用
wpf
姬激薄3 天前
配置Hadoop集群-集群配置
wpf
python算法(魔法师版)3 天前
.NET 在鸿蒙系统上的适配现状
华为od·华为·华为云·.net·wpf·harmonyos
大道随心3 天前
【wpf】11 在WPF中实现父窗口蒙版效果:原理详解与进阶优化
wpf
zizisuo4 天前
9.1.领域驱动设计
wpf
大道随心4 天前
【wpf】10 C#树形控件高效实现:递归构建与路径查找优化详解
开发语言·c#·wpf
离歌漠4 天前
WPF内嵌其他进程的窗口
c#·wpf
沉到海底去吧Go5 天前
【身份证识别表格】批量识别身份证扫描件或照片保存为Excel表格,怎么大批量将身份证图片转为excel表格?基于WPF和腾讯OCR的识别方案
ocr·wpf·excel·身份证识别表格·批量扫描件身份证转表格·图片识别表格·图片识别excel表格
csdn_aspnet5 天前
WPF 性能 UI 虚拟化 软件开发人员的思考
ui·wpf