J'apprends la liaison dans WPF. Je peux obtenir la liaison au travail lorsque 1) le texte d'un contrôle va directement au champ de texte d'un autre et 2) quand je configure manuellement la liaison dans le fichier code-behind.Lors de la définition d'une liaison purement en XAML, la variable source peut-elle être accédée dans le code derrière?
Dans le premier scénario, j'utilise purement XAML pour configurer la liaison. Est-il possible d'accéder à la variable source à partir de XAML dans le fichier code-behind?
<Window x:Class="DataBindingExperiments.MainWindow"
...
xmlns:local="clr-namespace:DataBindingExperiments.DataSources">
<Window.Resources>
<local:Person x:Key="MyPerson" />
</Window.Resources>
<Grid>
<StackPanel Orientation="Vertical">
<GroupBox Header="XAML Binding" Width="Auto" Height="110" Margin="5,5,5,5">
<Grid>
...
<Grid.DataContext>
<Binding Source="{StaticResource MyPerson}" />
</Grid.DataContext>
<TextBox Grid.Row="0" Grid.Column="1" Name="textBox_firstName" Text="{Binding Path=FirstName, UpdateSourceTrigger=PropertyChanged}" />
<TextBox Grid.Row="1" Grid.Column="1" Name="textBox_lastName" Text="{Binding Path=LastName, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Grid.Row="2" Grid.Column="1" Name="textBox_fullName" Text="{Binding Source={StaticResource MyPerson}, Path=FullName}" />
</Grid>
...
...
Dans le code ci-dessus, comment puis-je accéder à l'instance de 'myPerson' dans le code-behind?
@Thomas: Très intelligent, je ne connaissais pas la propriété DataItem :) – Pwninstein
moi non plus, je suis tombé) à ce sujet tout en recherchant BindingExpression dans la documentation ... –
@ThomasLevesque que pouvons-nous faire dans le cas de Silverlight ?? Je ne peux pas trouver GetBindingExpression pour silverlight –