Tout ce que j'essaie de faire est de lier une propriété publique à un texte textuel. Qu'est-ce que je fais mal ici? P> P>
3 Réponses :
Au début, vous avez besoin de votre classe à implémenter inotifyPropertychanganged a > ou une propriété pour être dépendanceProperty pour changer la valeur de la propriété Sur la textubox modification du texte, que vous ne pouvez se lier à cette propriété en donnant nom à cette fenêtre et en utilisant une propriété ElementName comme celle-ci. P> <Window x:Class="WpfApplication1.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" Name="myWindow">
<Window.Resources>
<ObjectDataProvider x:Key="test"></ObjectDataProvider>
</Window.Resources>
<Grid>
<TextBlock Height="23" HorizontalAlignment="Left" Margin="108,58,0,0" Name="textBlock1" VerticalAlignment="Top" Text="{Binding ElementName=myWindow, Path=test}" />
</Grid>
Vous pouvez simplement ajouter un DataContext et accéder à votre propriété
public partial class MainWindow : Window,INotifyPropertyChanged
{
private string _test;
public string test
{
get
{
return _test;
}
set
{
_test = value;
OnPropertyChanged("test");
}
}
public MainWindow()
{
test = "this is a test";
InitializeComponent();
DataContext = this;
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(String name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
<TextBlock Height="23" HorizontalAlignment="Left" Margin="108,58,0,0" Name="textBlock1" VerticalAlignment="Top" Text="{Binding test}"/>
Vous n'avez réellement besoin de mettre en œuvre inotifypropertychanged. Cependant, ce sera une liaison de données unique.
Par exemple dans XAML: P>
public string SomeProp { get; set; }
public MainWindow()
{
InitializeComponent();
SomeProp = "Test Test Test";
SomeTextBlock.DataContext = this;
}