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

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

TextBlock is not updating using ViewModel's property

问题

Sure, here is the translated content:

App.xaml.cs

public LogViewModel LogTextVm { get; set; }

ViewModelLocator.cs

App thisApp = (App)Application.Current;
public ViewModelLocator()
{
    thisApp.LogTextVm = new LogViewModel();
}

LogViewModel.cs

public class LogViewModel : INotifyPropertyChanged
{
    private string _logText { get; set; }
    public string LogText
    {
        get => _logText;
        set
        {
            if (_logText != value)
            {
                _logText = value;
                OnPropertyChanged("LogText");
            }
        }
    }
    private void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

MainWindow.xaml

xmlns:vm="clr-namespace:MyApplication.ViewModels;"
<Window.Resources>
    <vm:ViewModelLocator x:Key="Locator"/>
</Window.Resources>

<TextBlock Grid.Column="0" Text="{Binding LogText, Mode=TwoWay}" DataContext="{Binding LogViewModel, Source={StaticResource Locator}}"
           FontFamily="Segoe UI" FontWeight="SemiBold" FontStyle="Italic" Foreground="White" />

MainWindow.xaml.cs

private App thisApp = (App)Application.Current;
public MainWindow()
{
     InitializeComponent();
}
private async void CallGraphButton_Click(object sender, RoutedEventArgs e)
{
     thisApp.LogTextVm.LogText = "Status: Loading data...";
}

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

public LogViewModel LogTextVm { get; set; }

ViewModelLocator.cs

App thisApp = (App)Application.Current;
public ViewModelLocator()
{
    thisApp.LogTextVm = new LogViewModel();
}

LogViewModel.cs

public class LogViewModel : INotifyPropertyChanged
{
    private string _logText { get; set; }
    public string LogText
    {
        get =&gt; _logText;
        set
        {
            if(_logText!=value)
            {
                _logText = value;
                OnPropertyChnaged(&quot;LogText&quot;);
            }
        }
    }
    private void OnPropertyChnaged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

MainWindow.xaml

xmlns:vm=&quot;clr-namespace:MyApplication.ViewModels&quot;
&lt;Window.Resources&gt;
    &lt;vm:ViewModelLocator x:Key=&quot;Locator&quot;/&gt;
&lt;/Window.Resources&gt;

&lt;TextBlock Grid.Column=&quot;0&quot; Text=&quot;{Binding LogText, Mode=TwoWay}&quot; DataContext=&quot;{Binding LogViewModel, Source={StaticResource Locator}}&quot;
                       FontFamily=&quot;segoe UI&quot; FontWeight=&quot;SemiBold&quot; FontStyle=&quot;Italic&quot; Foreground=&quot;White&quot; /&gt;

MainWindow.xaml.cs

private App thisApp = (App)Application.Current;
public MainWindow()
{
     InitializeComponent();
}
private async void CallGraphButton_Click(object sender, RoutedEventArgs e)
{
     thisApp.LogTextVm.LogText = &quot;Status : Loading data ...&quot;;
}

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

public class ViewModelLocator
{
	public ViewModelLocator()
	{
		App thisApp = (App)Application.Current;
		LogTextVm = new LogViewModel();
		thisApp.LogTextVm = LogTextVm;
	}

	public LogViewModel LogTextVm { get; set; }

}

MainWindow.xaml

&lt;Grid&gt;
	&lt;StackPanel  DataContext=&quot;{Binding LogTextVm, Source={StaticResource Locator}}&quot;&gt;
		&lt;TextBlock  Text=&quot;label:&quot; /&gt;
		&lt;TextBlock  Text=&quot;{Binding LogText, Mode=TwoWay}&quot; /&gt;
		&lt;Button Content=&quot;click&quot; Click=&quot;Button_Click&quot; /&gt;
	&lt;/StackPanel&gt;
&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

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

public class ViewModelLocator
{
	public ViewModelLocator()
	{
		App thisApp = (App)Application.Current;
		LogTextVm = new LogViewModel();
		thisApp.LogTextVm = LogTextVm;
	}

	public LogViewModel LogTextVm { get; set; }

}

MainWindow.xaml

&lt;Grid&gt;
	&lt;StackPanel  DataContext=&quot;{Binding LogTextVm, Source={StaticResource Locator}}&quot;&gt;
		&lt;TextBlock  Text=&quot;label:&quot; /&gt;
		&lt;TextBlock  Text=&quot;{Binding LogText, Mode=TwoWay}&quot; /&gt;
		&lt;Button Content=&quot;click&quot; Click=&quot;Button_Click&quot; /&gt;
	&lt;/StackPanel&gt;
&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:

确定