如何正确将模型中的字符串绑定到 WPF 中的 TextBox?

huangapple go评论92阅读模式
英文:

How do I correctly bind a string in my model to an TextBox in WPF?

问题

我遇到了绑定错误,例如:

System.Windows.Data Error: 40 : BindingExpression 路径错误:在“object” “String” 上找不到“MyText”属性 (HashCode=-401799582)。 BindingExpression:Path=MyText; DataItem=“String” (HashCode=-401799582); 目标元素为“TextBox” (Name=“”); 目标属性为“Text” (类型“String”)

当尝试将我的模型的字符串属性绑定到TextBox和TextBlock时(上面的错误是TextBox的错误)。

我尝试松散地按照Microsoft网站上的 教程 进行操作。

在XAML中:

  1. <Window x:Class="WpfApp1.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:src="clr-namespace:WpfApp1"
  7. mc:Ignorable="d"
  8. Title="MainWindow" Height="450" Width="800">
  9. <Window.Resources>
  10. <src:MyView x:Key="myView"/>
  11. </Window.Resources>
  12. <Grid>
  13. <StackPanel>
  14. <Label>Input:</Label>
  15. <TextBox>
  16. <TextBox.Text>
  17. <Binding Source="{StaticResource myView}" Path="MyText" UpdateSourceTrigger="PropertyChanged"/>
  18. </TextBox.Text>
  19. </TextBox>
  20. <Label>In model:</Label>
  21. <TextBlock>
  22. <TextBlock.Text>
  23. <Binding Source="{StaticResource myView}" Path="MyText"/>
  24. </TextBlock.Text>
  25. </TextBlock>
  26. </StackPanel>
  27. </Grid>
  28. </Window>

在C#中:

  1. using System.ComponentModel;
  2. using System.Windows;
  3. namespace WpfApp1
  4. {
  5. public class MyView : INotifyPropertyChanged
  6. {
  7. public MyView() { }
  8. public event PropertyChangedEventHandler PropertyChanged;
  9. private string _myText;
  10. public string MyText
  11. {
  12. set
  13. {
  14. if (_myText != value)
  15. {
  16. _myText = value;
  17. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("MyText"));
  18. }
  19. }
  20. get { return _myText; }
  21. }
  22. }
  23. public partial class MainWindow : Window
  24. {
  25. public MainWindow()
  26. {
  27. InitializeComponent();
  28. }
  29. }
  30. }

我期望TextBlock在我在TextBox中输入文本时显示文本,但实际上没有任何反应(这是因为存在绑定错误)。我做错了什么?

英文:

I'm getting the binding errors, e.g.

> System.Windows.Data Error: 40 : BindingExpression path error: 'MyText' property not found on 'object' ''String' (HashCode=-401799582)'. BindingExpression:Path=MyText; DataItem='String' (HashCode=-401799582); target element is 'TextBox' (Name=''); target property is 'Text' (type 'String')

When trying to bind a string property of my model to a TextBox and a TextBlock (the error above is the one for the TextBox).

I've tried loosely following the how-to in Microsoft's site.

in xaml:

  1. &lt;Window x:Class=&quot;WpfApp1.MainWindow&quot;
  2. xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  3. xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
  4. xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
  5. xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
  6. xmlns:src=&quot;clr-namespace:WpfApp1&quot;
  7. mc:Ignorable=&quot;d&quot;
  8. Title=&quot;MainWindow&quot; Height=&quot;450&quot; Width=&quot;800&quot;&gt;
  9. &lt;Window.Resources&gt;
  10. &lt;src:MyView x:Key=&quot;myView&quot;/&gt;
  11. &lt;/Window.Resources&gt;
  12. &lt;Grid&gt;
  13. &lt;StackPanel&gt;
  14. &lt;Label&gt;Input:&lt;/Label&gt;
  15. &lt;TextBox&gt;
  16. &lt;TextBox.Text&gt;
  17. &lt;Binding Source=&quot;StaticResource myView&quot; Path=&quot;MyText&quot; UpdateSourceTrigger=&quot;PropertyChanged&quot;/&gt;
  18. &lt;/TextBox.Text&gt;
  19. &lt;/TextBox&gt;
  20. &lt;Label&gt;In model:&lt;/Label&gt;
  21. &lt;TextBlock&gt;
  22. &lt;TextBlock.Text&gt;
  23. &lt;Binding Source=&quot;StaticResource myView&quot; Path=&quot;MyText&quot;/&gt;
  24. &lt;/TextBlock.Text&gt;
  25. &lt;/TextBlock&gt;
  26. &lt;/StackPanel&gt;
  27. &lt;/Grid&gt;
  28. &lt;/Window&gt;

in C#:

  1. using System.ComponentModel;
  2. using System.Windows;
  3. namespace WpfApp1
  4. {
  5. public class MyView : INotifyPropertyChanged
  6. {
  7. public MyView() { }
  8. public event PropertyChangedEventHandler PropertyChanged;
  9. private string _myText;
  10. public string MyText
  11. {
  12. set
  13. {
  14. if (_myText != value)
  15. {
  16. _myText = value;
  17. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(&quot;MyText&quot;));
  18. }
  19. }
  20. get { return _myText; }
  21. }
  22. }
  23. public partial class MainWindow : Window
  24. {
  25. public MainWindow()
  26. {
  27. InitializeComponent();
  28. }
  29. }
  30. }

I was expecting the TextBlock to display my text as I type it in the TextBox, but it runs and nothing happens (which makes sense seeing as there was a binding error).

What Am I doing wrong?

答案1

得分: 1

在字符串中编写StaticResource并不意味着您已引用该资源。StaticResource 是一个标记扩展,具有自己的语法(花括号):

  1. <StackPanel>
  2. <Label>Input:</Label>
  3. <TextBox>
  4. <TextBox.Text>
  5. <Binding Source="{StaticResource myView}" Path="MyText" UpdateSourceTrigger="PropertyChanged"/>
  6. </TextBox.Text>
  7. </TextBox>
  8. <Label>In model:</Label>
  9. <TextBlock>
  10. <TextBlock.Text>
  11. <Binding Source="{StaticResource myView}" Path="MyText"/>
  12. </TextBlock.Text>
  13. </TextBlock>
  14. </StackPanel>

您还可以以更简洁的方式编写相同的标记:

  1. <StackPanel>
  2. <Label>Input:</Label>
  3. <TextBox Text="{Binding Path=MyText, Source={StaticResource myView}, UpdateSourceTrigger=PropertyChanged}"/>
  4. <Label>In model:</Label>
  5. <TextBlock Text="{Binding Path=MyText, Source={StaticResource myView}}"/>
  6. </StackPanel>

或者甚至更好 - 声明 DataContext,而不是资源:

  1. <Window x:Class="WpfApp1.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  5. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  6. xmlns:src="clr-namespace:WpfApp1"
  7. mc:Ignorable="d"
  8. Title="MainWindow" Height="450" Width="800">
  9. <Window.DataContext>
  10. <src:MyView />
  11. </Window.DataContext>
  12. <Grid>
  13. <StackPanel>
  14. <Label>Input:</Label>
  15. <TextBox Text="{Binding MyText, UpdateSourceTrigger=PropertyChanged}"/>
  16. <Label>In model:</Label>
  17. <TextBlock Text="{Binding MyText}"/>
  18. </StackPanel>
  19. </Grid>
  20. </Window>
英文:

writing StaticResource in a string doesn't mean that you referenced that resource. StaticResource is a markup extension and has its own syntax (curly braces):

  1. &lt;StackPanel&gt;
  2. &lt;Label&gt;Input:&lt;/Label&gt;
  3. &lt;TextBox&gt;
  4. &lt;TextBox.Text&gt;
  5. &lt;Binding Source=&quot;{StaticResource myView}&quot; Path=&quot;MyText&quot; UpdateSourceTrigger=&quot;PropertyChanged&quot;/&gt;
  6. &lt;/TextBox.Text&gt;
  7. &lt;/TextBox&gt;
  8. &lt;Label&gt;In model:&lt;/Label&gt;
  9. &lt;TextBlock&gt;
  10. &lt;TextBlock.Text&gt;
  11. &lt;Binding Source=&quot;{StaticResource myView}&quot; Path=&quot;MyText&quot;/&gt;
  12. &lt;/TextBlock.Text&gt;
  13. &lt;/TextBlock&gt;
  14. &lt;/StackPanel&gt;

you can also write the same markup in significantly less verbose way:

  1. &lt;StackPanel&gt;
  2. &lt;Label&gt;Input:&lt;/Label&gt;
  3. &lt;TextBox Text=&quot;{Binding Path=MyText, Source={StaticResource myView}, UpdateSourceTrigger=PropertyChanged}&quot;/&gt;
  4. &lt;Label&gt;In model:&lt;/Label&gt;
  5. &lt;TextBlock Text=&quot;{Binding Path=MyText, Source={StaticResource myView}}&quot;/&gt;
  6. &lt;/StackPanel&gt;

or even better - declare DataContext, not a Resource:

  1. &lt;Window x:Class=&quot;WpfApp1.MainWindow&quot;
  2. xmlns=&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  3. xmlns:x=&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;
  4. xmlns:d=&quot;http://schemas.microsoft.com/expression/blend/2008&quot;
  5. xmlns:mc=&quot;http://schemas.openxmlformats.org/markup-compatibility/2006&quot;
  6. xmlns:src=&quot;clr-namespace:WpfApp1&quot;
  7. mc:Ignorable=&quot;d&quot;
  8. Title=&quot;MainWindow&quot; Height=&quot;450&quot; Width=&quot;800&quot;&gt;
  9. &lt;Window.DataContext&gt;
  10. &lt;src:MyView /&gt;
  11. &lt;/Window.DataContext&gt;
  12. &lt;Grid&gt;
  13. &lt;StackPanel&gt;
  14. &lt;Label&gt;Input:&lt;/Label&gt;
  15. &lt;TextBox Text=&quot;{Binding MyText, UpdateSourceTrigger=PropertyChanged}&quot;/&gt;
  16. &lt;Label&gt;In model:&lt;/Label&gt;
  17. &lt;TextBlock Text=&quot;{Binding MyText}&quot;/&gt;
  18. &lt;/StackPanel&gt;
  19. &lt;/Grid&gt;
  20. &lt;/Window&gt;

huangapple
  • 本文由 发表于 2023年2月19日 20:56:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75500293.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定