2010-07-21 18 views
3

Je veux lier la propriété de chaîne avec Button.Content.liaison WPF de la propriété de chaîne et Button.Content

Mais pourquoi cela n'a pas fonctionné?

La classe de données:

namespace test4 
{ 
    public class Test : INotifyPropertyChanged 
    { 

     string _Text = "Begin"; 

     public string Text 
     { 
      get{return _Text;} 
      protected set { _Text = value; } 
     } 
     public void Start() 
     { 
      Text = "Begin"; 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(Text)); 
     } 
     public void End() 
     { 
      Text = "End"; 
      if (PropertyChanged != null) 
       PropertyChanged(this, new PropertyChangedEventArgs(Text)); 
     } 


     public event PropertyChangedEventHandler PropertyChanged; 
    } 
} 

Le code logique:

public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 
     } 
     test4.Test ttt = new test4.Test(); 
     private void Window_Loaded(object sender, RoutedEventArgs e) 
     { 

      System.Windows.Data.CollectionViewSource testViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("testViewSource"))); 
      testViewSource.Source = new object[]{ttt}; 
     } 

     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      if (ttt.Text == "Begin") 
       ttt.End(); 
      else 
       ttt.Start(); 
     } 
    } 

Le XAML:

<Window x:Class="test5.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:my="clr-namespace:test4" Loaded="Window_Loaded"> 
    <Window.Resources> 
     <CollectionViewSource x:Key="testViewSource" d:DesignSource="{d:DesignInstance my:Test, CreateList=true}" /> 
    </Window.Resources> 
    <Grid DataContext="{StaticResource testViewSource}"> 
     <Button Content="{Binding Path=Text, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Height="23" HorizontalAlignment="Left" Margin="158,95,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> 
    </Grid> 
</Window> 

Répondre

3

Les citations autour du texte dans l'appel au constructeur de PropertyChangedEventArgs sont manquants:

Text = "Begin"; 
if (PropertyChanged != null) 
    PropertyChanged(this, new PropertyChangedEventArgs("Text")); 
2

PropertyChanged veut voir le nom de la propriété qui a changé et non la valeur. Changez l'invocation d'événement à:

PropertyChanged(this, new PropertyChangedEventArgs("Text")); 

et il fera le travail. Cependant, je changerais aussi la construction de

public string Text { 
    get{return _Text;} 
    protected set { 
      _Text = value; 
      if(null != PropertyChanged){ 
       PropertyChanged(this,new PropertyChangedEventArgs("Text")); 
      } 
    } 
} 

et n'appelez pas le PropertyChanged événement de début et de fin.

Et pour aller encore plus loin, créer invocation des méthodes telles que:

protected virtual void OnPropertyChanged(string propertyName) { 
    OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); 
} 

protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) { 
    if (null != PropertyChanged) { 
     PropertyChanged(this,e); 
    } 
} 

puis les appeler de votre propriété compositeur.

0

Tout d'abord, j'objectdataproviding, mais ce ne sont pas l'objet « TTT », ce sont deux choses différentes.

Deuxième, PropertyChanged(this,new PropertyChangedEventArgs("Text")); "Texte" est le nom et non la variable. Donc, le code suit, peut être utile à d'autres.

La classe de données:

namespace test3 
{ 
    public class Test : INotifyPropertyChanged 
    { 

     string _Text = "Begin"; 

     public string Text 
     { 
      get{return _Text;} 
      protected set { _Text = value; 
      NotifyPropertyChanged("Text"); 
      } 
     } 
     public void Start() 
     { 
      Text = "Begin"; 

     } 
     public void End() 
     { 
      Text = "End"; 

     } 

     public event PropertyChangedEventHandler PropertyChanged; 
     public void NotifyPropertyChanged(string propertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 
} 

Le CS logique:

public partial class MainWindow : Window 
    { 
     public MainWindow() 
     { 
      InitializeComponent(); 

      button1.DataContext=ttt; 
     } 

     Test ttt = new Test(); 

     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      if (ttt.Text == "Begin") 
       ttt.End(); 
      else 
       ttt.Start(); 
     } 
    } 
} 

Le XAML:

<Window x:Class="test3.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:test3" 
     Title="MainWindow" Height="350" Width="525" > 
    <Grid> 
     <Button Content="{Binding Path=Text,UpdateSourceTrigger=PropertyChanged}" Height="23" HorizontalAlignment="Left" Margin="121,69,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> 
    </Grid> 
</Window> 
1

Vous devez utiliser des guillemets autour de la propriété:

public void Start() 
{ 
    Text = "Begin"; 
    if (PropertyChanged != null) 
     PropertyChanged(this, new PropertyChangedEventArgs("Text")); 
} 

Je suggère d'utiliser plutôt le NomDe() - méthode que vous ne pouvez pas renommer la propriété alors qu'il est situé entre guillemets:

public void Start() 
{ 
    Text = "Begin"; 
    if (PropertyChanged != null) 
     PropertyChanged(this, new PropertyChangedEventArgs(nameof(Text))); 
}