C# WPF绑定-标签绑定问题

C# WPF绑定-标签绑定问题,c#,wpf,mvvm,datacontext,C#,Wpf,Mvvm,Datacontext,我试图使用异步数据绑定,这是因为我需要在我的应用程序上执行几个线程,这些线程必须能够访问UI控件。为此,我将标签声明为: <Label x:Name="SyncRange" Content="{Binding NextSynchronization, IsAsync=True}" /> 如何查看nextSync的默认值是N/A,我可以从任何类更改变量的值。 此时,我已通过以下方式在主窗口中导入控件: xmlns:OwnControl="clr-namespace:Synchroni

我试图使用
异步数据绑定
,这是因为我需要在我的应用程序上执行几个线程,这些线程必须能够访问UI控件。为此,我将
标签声明为:

<Label x:Name="SyncRange" Content="{Binding NextSynchronization, IsAsync=True}" />
如何查看
nextSync
的默认值是
N/A
,我可以从任何类更改变量的值。 此时,我已通过以下方式在主窗口中导入控件:

xmlns:OwnControl="clr-namespace:SynchronizationTool"
并将其用作:

<OwnControl:Scheduler x:Name="Scheduler"/>
标签应该自动绑定值
test
,但不幸的是,标签仍然是空的。我做错了什么

更新

我已经在我的控件中创建了一个
Test
类:

public class Test {
    private string nextSync = "N/A";

    public string NextSynchronization {
        get {
            return nextSync;
        }
        set {
            nextSync = value;
        }
    }
}
主窗口中
我使用以下命令:

DataContext = new CScheduler.Test();

似乎标签已正确初始化为
N/A

它只是一条注释。首先,您应该在此行上放置一个断点:

return nextSync;
但我最想知道的是,为什么在“set”(您的类实现了INotifyPropertyChanged)之后没有任何OnPropertyChanged


这只是一个评论。首先,您应该在此行上放置一个断点:

return nextSync;
但我最想知道的是,为什么在“set”(您的类实现了INotifyPropertyChanged)之后没有任何OnPropertyChanged


下面是一个正确设置DataContext的示例:

<Window x:Class="TestApplication.TestWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"
    Title="TestWindow" Height="300" Width="300">
<Grid>
    <TextBox Text="{Binding Text}"></TextBox>
    <CheckBox IsChecked="{Binding IsChecked}"></CheckBox>
    <Button Content="{Binding BtnText}"></Button>
</Grid>
定义ViewModelClass:

public class MyViewModelClass: INotifyPropertyChanged
{
    //Add Constructor
    public MyViewModelClass()
    {

    }


    private string _text = "sampleText shown in the TextBox";

    public string Text
    {
        get { return _text; }
        set 
        {
            nextSync = value;
            OnPropertyChanged();//PropertyName will be passed automatically
        }
    }

    private string _isChecked = true;//CheckBox is checked by default

    public string IsChecked
    {
        get { return _isChecked ; }
        set 
        {
            nextSync = value;
            OnPropertyChanged();//PropertyName will be passed automatically
        }
    }

    private string _btnText = "Click Me";//Text to display on the button

    public string BtnText
    {
        get { return _btnText ; }
        set 
        {
            nextSync = value;
            OnPropertyChanged();//PropertyName will be passed automatically
        }
    }



    #region Implementation of INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    //When using the [CallerMemberName] Attribute you dont need to pass the PropertyName to the method which is pretty nice :D
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion Implementation of INotifyPropertyChanged
}
基本上,
DataContext
告诉UI在哪里查找绑定。 如果未设置此选项,则在生成时,VisualStuido的输出窗口中将显示绑定错误

更新:

如果使用
用户控件

在项目中添加一个名为UserControls的文件夹,在其中放置xamls。 在窗口中为它们添加命名空间:

xmlns:userControls="clr-namespace:<YourApplicationName>.UserControls"
下面是重要的部分:

  • 如果未在UserControl.xaml.cs中设置UserControl的DataContext 它将自动使用来自父控件的控件(在您的情况下是窗口)
  • 因此,在window或usercontrols中获得的所有绑定都放在该1 ViewModel中
  • 仅为窗口设置
    DataContext
  • 现在,所有绑定都应该取自该viewmodel

下面是一个正确设置DataContext的示例:

<Window x:Class="TestApplication.TestWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d"
    Title="TestWindow" Height="300" Width="300">
<Grid>
    <TextBox Text="{Binding Text}"></TextBox>
    <CheckBox IsChecked="{Binding IsChecked}"></CheckBox>
    <Button Content="{Binding BtnText}"></Button>
</Grid>
定义ViewModelClass:

public class MyViewModelClass: INotifyPropertyChanged
{
    //Add Constructor
    public MyViewModelClass()
    {

    }


    private string _text = "sampleText shown in the TextBox";

    public string Text
    {
        get { return _text; }
        set 
        {
            nextSync = value;
            OnPropertyChanged();//PropertyName will be passed automatically
        }
    }

    private string _isChecked = true;//CheckBox is checked by default

    public string IsChecked
    {
        get { return _isChecked ; }
        set 
        {
            nextSync = value;
            OnPropertyChanged();//PropertyName will be passed automatically
        }
    }

    private string _btnText = "Click Me";//Text to display on the button

    public string BtnText
    {
        get { return _btnText ; }
        set 
        {
            nextSync = value;
            OnPropertyChanged();//PropertyName will be passed automatically
        }
    }



    #region Implementation of INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    //When using the [CallerMemberName] Attribute you dont need to pass the PropertyName to the method which is pretty nice :D
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion Implementation of INotifyPropertyChanged
}
基本上,
DataContext
告诉UI在哪里查找绑定。 如果未设置此选项,则在生成时,VisualStuido的输出窗口中将显示绑定错误

更新:

如果使用
用户控件

在项目中添加一个名为UserControls的文件夹,在其中放置xamls。 在窗口中为它们添加命名空间:

xmlns:userControls="clr-namespace:<YourApplicationName>.UserControls"
下面是重要的部分:

  • 如果未在UserControl.xaml.cs中设置UserControl的DataContext 它将自动使用来自父控件的控件(在您的情况下是窗口)
  • 因此,在window或usercontrols中获得的所有绑定都放在该1 ViewModel中
  • 仅为窗口设置
    DataContext
  • 现在,所有绑定都应该取自该viewmodel


自从我使用wpf以来,这一直是一个诡计,因此我不确定这是否仍然是必需的做法,但通常对于viewmodel来说,它通知视图它必须继承的更改
INotifyPropertyChanged
。仍然是这样吗?您是否在
main window.xaml.cs
中正确设置了
DataContext
?@Nkosi yap,这仍然是需要的@FeDe好吧,我的坏,似乎有点编辑我可以让它工作,检查我的更新是一个很好的开始点!自从我使用wpf以来,这一直是一个诡计,因此我不确定这是否仍然是必需的做法,但通常对于viewmodel来说,通知视图它必须继承的更改
INotifyPropertyChanged
。仍然是这样吗?您是否在
main window.xaml.cs
中正确设置了
DataContext
?@Nkosi yap,这仍然是需要的@FeDe好吧,我的坏,似乎有点编辑我可以让它工作,检查我的更新是一个很好的开始点!这声音好极了!但是
INotifyPropertyChanged
有什么好处呢?
INotifyPropertyChanged
告诉用户界面
属性的
已经更改,用户界面需要更新该属性。请参阅以了解详细信息基本上:INotifyPropertyChanged帮助您通知客户端:基本上,UI将知道已将新值放入绑定属性中。因此UI能够实现自身(通过调用get)DataContext=Scheduler.NextSynchronization=“Test”标签没有更新值,并且仍然是空的,我做错了什么?这听起来很棒!但是
INotifyPropertyChanged
有什么好处呢?
INotifyPropertyChanged
告诉用户界面
属性的
已经更改,用户界面需要更新该属性。请参阅以了解详细信息基本上:INotifyPropertyChanged帮助您通知客户端:基本上,UI将知道已将新值放入绑定属性中。因此UI能够实现自身(通过调用get)DataContext=Scheduler.NextSynchronization=“Test”
标签不更新值,而且仍然为空,我做错了什么?
MyViewModelClass
应该包含每个控件的所有方法吗?我想我会创建一个接口或其他东西
<userControls:MyUserControl1></userControls:MyUserControl1>