TextBlock不使用ViewModel的属性进行更新。

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

TextBlock is not updating using ViewModel's property

问题

Sure, here is the translated content:

App.xaml.cs

  1. public LogViewModel LogTextVm { get; set; }

ViewModelLocator.cs

  1. App thisApp = (App)Application.Current;
  2. public ViewModelLocator()
  3. {
  4. thisApp.LogTextVm = new LogViewModel();
  5. }

LogViewModel.cs

  1. public class LogViewModel : INotifyPropertyChanged
  2. {
  3. private string _logText { get; set; }
  4. public string LogText
  5. {
  6. get => _logText;
  7. set
  8. {
  9. if (_logText != value)
  10. {
  11. _logText = value;
  12. OnPropertyChanged("LogText");
  13. }
  14. }
  15. }
  16. private void OnPropertyChanged(string propertyName)
  17. {
  18. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  19. }
  20. public event PropertyChangedEventHandler PropertyChanged;
  21. }

MainWindow.xaml

  1. xmlns:vm="clr-namespace:MyApplication.ViewModels;"
  2. <Window.Resources>
  3. <vm:ViewModelLocator x:Key="Locator"/>
  4. </Window.Resources>
  5. <TextBlock Grid.Column="0" Text="{Binding LogText, Mode=TwoWay}" DataContext="{Binding LogViewModel, Source={StaticResource Locator}}"
  6. FontFamily="Segoe UI" FontWeight="SemiBold" FontStyle="Italic" Foreground="White" />

MainWindow.xaml.cs

  1. private App thisApp = (App)Application.Current;
  2. public MainWindow()
  3. {
  4. InitializeComponent();
  5. }
  6. private async void CallGraphButton_Click(object sender, RoutedEventArgs e)
  7. {
  8. thisApp.LogTextVm.LogText = "Status: Loading data...";
  9. }

This is the translated code portion you provided.

英文:

I am working on a small wpf application, i am showing status using a textblock and for achieving this i have bind a property from a view model. I have mapped everything despite this UI is not showing any value. I am pasting code as below.

App.xaml.cs

  1. public LogViewModel LogTextVm { get; set; }

ViewModelLocator.cs

  1. App thisApp = (App)Application.Current;
  2. public ViewModelLocator()
  3. {
  4. thisApp.LogTextVm = new LogViewModel();
  5. }

LogViewModel.cs

  1. public class LogViewModel : INotifyPropertyChanged
  2. {
  3. private string _logText { get; set; }
  4. public string LogText
  5. {
  6. get =&gt; _logText;
  7. set
  8. {
  9. if(_logText!=value)
  10. {
  11. _logText = value;
  12. OnPropertyChnaged(&quot;LogText&quot;);
  13. }
  14. }
  15. }
  16. private void OnPropertyChnaged(string propertyName)
  17. {
  18. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  19. }
  20. public event PropertyChangedEventHandler PropertyChanged;
  21. }

MainWindow.xaml

  1. xmlns:vm=&quot;clr-namespace:MyApplication.ViewModels&quot;
  2. &lt;Window.Resources&gt;
  3. &lt;vm:ViewModelLocator x:Key=&quot;Locator&quot;/&gt;
  4. &lt;/Window.Resources&gt;
  5. &lt;TextBlock Grid.Column=&quot;0&quot; Text=&quot;{Binding LogText, Mode=TwoWay}&quot; DataContext=&quot;{Binding LogViewModel, Source={StaticResource Locator}}&quot;
  6. FontFamily=&quot;segoe UI&quot; FontWeight=&quot;SemiBold&quot; FontStyle=&quot;Italic&quot; Foreground=&quot;White&quot; /&gt;

MainWindow.xaml.cs

  1. private App thisApp = (App)Application.Current;
  2. public MainWindow()
  3. {
  4. InitializeComponent();
  5. }
  6. private async void CallGraphButton_Click(object sender, RoutedEventArgs e)
  7. {
  8. thisApp.LogTextVm.LogText = &quot;Status : Loading data ...&quot;;
  9. }

Can anyone help me, where i am doing mistake?

答案1

得分: 2

You have to assign MainWindow.DataContext with the same LogViewModel instance that is created by ViewModelLocator.

DataContext=&quot;{Binding LogViewModel, Source={StaticResource Locator}}&quot; can it create a new instance of the ViewModelLocator (and therefore thus new LogViewModel)?

Please put the breakpoint at the thisApp.LogTextVm = new LogViewModel(); line and check is it executed twice?

UPD VS output shows binding error for your code

System.Windows.Data Error: 40 : BindingExpression path error: &#39;LogViewModel&#39; property not found on &#39;object&#39; &#39;&#39;ViewModelLocator&#39; (HashCode=37320431)&#39;. BindingExpression:Path=LogViewModel; DataItem=&#39;ViewModelLocator&#39; (HashCode=37320431); target element is &#39;StackPanel&#39; (Name=&#39;&#39;); target property is &#39;DataContext&#39; (type &#39;Object&#39;)

You are binding to the wrong names unfortunately. To fix the binding I had to change the code as below:

ViewModelLocator

  1. public class ViewModelLocator
  2. {
  3. public ViewModelLocator()
  4. {
  5. App thisApp = (App)Application.Current;
  6. LogTextVm = new LogViewModel();
  7. thisApp.LogTextVm = LogTextVm;
  8. }
  9. public LogViewModel LogTextVm { get; set; }
  10. }

MainWindow.xaml

  1. &lt;Grid&gt;
  2. &lt;StackPanel DataContext=&quot;{Binding LogTextVm, Source={StaticResource Locator}}&quot;&gt;
  3. &lt;TextBlock Text=&quot;label:&quot; /&gt;
  4. &lt;TextBlock Text=&quot;{Binding LogText, Mode=TwoWay}&quot; /&gt;
  5. &lt;Button Content=&quot;click&quot; Click=&quot;Button_Click&quot; /&gt;
  6. &lt;/StackPanel&gt;
  7. &lt;/Grid&gt;

DataContext=&quot;{Binding LogTextVm, Source={StaticResource Locator}}&quot;&gt; creates ViewModelLocator instance and binds StackPanel.DataContext to the ViewModelLocator.LogTextVm property.

The &lt;TextBlock Text=&quot;{Binding LogText, Mode=TwoWay}&quot; /&gt; binds to the LogText for the current DataContext (which is LogTextVm property value).

英文:

You have to assign MainWindow.DataContext with the same LogViewModel instance that is created by ViewModelLocator

DataContext=&quot;{Binding LogViewModel, Source={StaticResource Locator}}&quot; can it create a new instance of the ViewModelLocator (and therefore thus new LogViewModel) ?

Please put the breakpoint at the thisApp.LogTextVm = new LogViewModel(); line and check is it executed twice?

UPD VS output shows binding error for your code

  1. System.Windows.Data Error: 40 : BindingExpression path error: &#39;LogViewModel&#39; property not found on &#39;object&#39; &#39;&#39;ViewModelLocator&#39; (HashCode=37320431)&#39;. BindingExpression:Path=LogViewModel; DataItem=&#39;ViewModelLocator&#39; (HashCode=37320431); target element is &#39;StackPanel&#39; (Name=&#39;&#39;); target property is &#39;DataContext&#39; (type &#39;Object&#39;)

You are binding to the wrong names unfortunately. To fix the binding I had to change the code as below:

ViewModelLocator

  1. public class ViewModelLocator
  2. {
  3. public ViewModelLocator()
  4. {
  5. App thisApp = (App)Application.Current;
  6. LogTextVm = new LogViewModel();
  7. thisApp.LogTextVm = LogTextVm;
  8. }
  9. public LogViewModel LogTextVm { get; set; }
  10. }

MainWindow.xaml

  1. &lt;Grid&gt;
  2. &lt;StackPanel DataContext=&quot;{Binding LogTextVm, Source={StaticResource Locator}}&quot;&gt;
  3. &lt;TextBlock Text=&quot;label:&quot; /&gt;
  4. &lt;TextBlock Text=&quot;{Binding LogText, Mode=TwoWay}&quot; /&gt;
  5. &lt;Button Content=&quot;click&quot; Click=&quot;Button_Click&quot; /&gt;
  6. &lt;/StackPanel&gt;
  7. &lt;/Grid&gt;

DataContext=&quot;{Binding LogTextVm, Source={StaticResource Locator}}&quot;&gt; creates ViewModelLocator instance and binds StackPanel.DataContext to the ViewModelLocator.LogTextVm property.

The &lt;TextBlock Text=&quot;{Binding LogText, Mode=TwoWay}&quot; /&gt; binds to the LogText for the current DataContext (which is LogTextVm property value)

huangapple
  • 本文由 发表于 2020年1月6日 16:58:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609172.html
匿名

发表评论

匿名网友

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

确定