简体   繁体   English

属性更改事件未触发WPF

[英]Property changed event is not getting fired wpf

I have to change the value in a text box dynamically, on selecting a value from a combox box, which is present in different view. 从组合框中选择一个值(在不同视图中显示)后,我必须动态地更改文本框中的值。 when changing the dependency property's source, the propertychangedEventHandler value is not changing, ie it is remaining as null, so the event is not getting fired. 更改依赖项属性的源时,propertychangedEventHandler值未更改,即,它保持为null,因此不会触发该事件。 As a result the text in the textbox is not changing. 结果,文本框中的文本没有更改。 Below is the code. 下面是代码。 I have bound the text in textbox to _name property. 我已经将文本框中的文本绑定到_name属性。

public partial class Details : UserControl, INotifyPropertyChanged
{

   public event PropertyChangedEventHandler PropertyChanged;
   public string name = "";

     public Details()
     {
       InitializeComponent();
       Name = Connector.Name;
       DataContext = this;
     }


    public string Name
    {
       get { return name; }
       set
       {
           name = value; OnPropertyChanged("Name");
       }
   }

   protected void OnPropertyChanged(string s)
   {
         PropertyChangedEventHandler handler = PropertyChanged;
         if (handler != null)
         {
             handler(this, new PropertyChangedEventArgs(s));
         }
   }
}

Xaml code XAML代码

    <StackPanel Orientation="Vertical">

            <TextBlock Text="Student Details" VerticalAlignment="Top" HorizontalAlignment="Center" FontSize="16" FontWeight="Bold">  </TextBlock>

            <StackPanel Margin="0,5" Orientation="Horizontal" >

        <Label MinWidth="100" MaxWidth="110">Name:</Label>

                <Border BorderBrush="Gray" BorderThickness="2">

                    <TextBox Name="nametextbox" Text="{Binding Name,Mode=TwoWay}"  Width="auto" MinWidth="100" FontWeight="Black"></TextBox>
                </Border>

            </StackPanel>

Is it possible that you accidentally exchanged name and _name , using name in XAML for the binding? 您是否有可能在XAML中使用name进行绑定时意外交换了name_name

Usually you have a public property with a capitalized name, and a private field with a non-capitalized name, optionally prefixed with an underscore as you did. 通常,您有一个带有大写字母名称的公共属性,以及一个带有非大写字母名称的私有字段,并且可以像您一样在前面加上下划线。

So, you should have 所以,你应该有

public string Name {
    get { return _name; }
    set { _name = value; OnPropertyChanged("Name"); }
{

private string _name = "";

Please check the following: 请检查以下内容:

  • If you're not currently binding to name instead of _name ; 如果您当前未绑定到name而不是_name
  • Either if that is or is not the case, please fix your naming convention, because it is a source of errors, and every example you'll find follow the convention I included above. 不管是不是事实,都请修正您的命名约定,因为它是错误的来源,并且您会发现每个示例都遵循我上面包含的约定。

In your XAML, you are binding "Name" property and in your code, you have created _name property. 在XAML中,您将绑定“ Name”属性,并在代码中创建了_name属性。 So, you need to change it to "Name" property in your code. 因此,您需要在代码中将其更改为“ Name”属性。

Just change your property as per below: 只需按照以下方式更改您的财产:

private string _name = "";

public string Name 
        {
    get { return _name; }
    set { 
         _name = value; 
         OnPropertyChanged("Name"); 
        }
}

Try this and let me know. 试试这个,让我知道。

I have used eventaggregator for this purpose, as we need to change the text in the text box dynamically when an event in a different view is fired. 我已经使用eventaggregator来实现此目的,因为当触发另一个视图中的事件时,我们需要动态更改文本框中的文本。 Below is the C# code of both the DropView(where we select student name from a list), and DetailsView(where we display the details). 下面是DropView(从列表中选择学生姓名)和DetailsView(在其中显示详细信息)的C#代码。 I publish events in Drop.xaml.cs and subscribe to those events in Details.xaml.cs 我在Drop.xaml.cs中发布事件,并在Details.xaml.cs中订阅这些事件

Drop.xaml.cs Drop.xaml.cs

public partial class Drop : UserControl { 公共局部类Drop:UserControl {

    private IEventAggregator iEventAggregator;

    public Drop(IEventAggregator ieventaggregator)
    {
        InitializeComponent();
        iEventAggregator = ieventaggregator;
        this.DataContext = this;
        var doc = XDocument.Load("C:\\Users\\srinivasaarudra.k\\Desktop\\students.xml");           
        var names = doc.Descendants("Name");
        foreach (var item in names)
        {
            droplist.Items.Add(item.Value);
        }

    }
    public string name;
    public string Naam
    {
        get { return name; }
        set { name = value;
        iEventAggregator.GetEvent<Itemselectedevent>().Publish(Naam);
        }

    }
    public string grade;
    public string Grade
    {
        get { return grade; }
        set
        {
            grade = value;
            iEventAggregator.GetEvent<gradeevent>().Publish(Grade);
        }
    }
    public string dept;
    public string Dept
    {
        get { return dept; }
        set
        {
            dept = value;
            iEventAggregator.GetEvent<deptevent>().Publish(Dept);
        }
    }
    public static string str;
    public static string Str
    {
        get { return str; }
        set {
            str = value;

             }
    }

    private void droplist_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      var sel = droplist.SelectedValue;
      Str=sel.ToString();
      XmlDocument doc2 = new XmlDocument();
      doc2.Load("C:\\Users\\srinivasaarudra.k\\Desktop\\students.xml");
      var details = doc2.DocumentElement.SelectNodes("/Students/StudentDetails");
      foreach (XmlNode node in details)
      {
          if (node.SelectSingleNode("Name").InnerText == Str)
          {
              Naam = node.SelectSingleNode("Name").InnerText;
              Grade = node.SelectSingleNode("Grade").InnerText;
              Dept = node.SelectSingleNode("Department").InnerText;

          }

      }
    //  Details det = new Details();  
      Details dt = new Details(iEventAggregator);

    }


}
 public class Itemselectedevent:Prism.Events.PubSubEvent<string>
    {

    }
 public class gradeevent : Prism.Events.PubSubEvent<string>
 {

 }
 public class deptevent : Prism.Events.PubSubEvent<string>
 {
 }

Details.xaml.cs Details.xaml.cs

public partial class Details : UserControl,INotifyPropertyChanged { 公共局部类的详细信息:UserControl,INotifyPropertyChanged {

    public IEventAggregator iEventAggregator;

   public  event PropertyChangedEventHandler PropertyChanged;

   public static string name;
   public static string dept;
   public static string grade;

   [Bindable(true)]
   public  string Naam
   {
       get { return name; }
       set
       { 
        name = value;
        OnPropertyChanged("Naam");
       }

   }
   [Bindable(true)]
   public string Grade
   {
       get { return grade; }
       set
       {

               grade = value; OnPropertyChanged("Grade");

       }
   }
   [Bindable(true)]
   public string Dept
   {
       get { return dept; }
       set
       {

               dept = value;
               OnPropertyChanged("Dept");

       }
   }
   public Details(IEventAggregator eventaggregator)
   {
       InitializeComponent();
       this.iEventAggregator = eventaggregator;
       iEventAggregator.GetEvent<Itemselectedevent>().Subscribe((str) => { Naam = str; });
       iEventAggregator.GetEvent<gradeevent>().Subscribe((str) => { Grade = str; });
       iEventAggregator.GetEvent<deptevent>().Subscribe((str) => { Dept = str; });          
       this.DataContext = this;           
   }

   protected void OnPropertyChanged(string s)
   {
         PropertyChangedEventHandler handler = PropertyChanged;          
         if (handler != null)
         {
             handler(this, new PropertyChangedEventArgs(s));
         }            
   }
   private void Button_Click_1(object sender, RoutedEventArgs e)
   {  
     Application.Current.Shutdown();
   }      
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM