2010-06-09 4 views
1

J'ai un problème pour déplacer une liaison XMLDataprovider avec XPath de Xaml vers le code derrière.Liaison à XMLDataProvider dans le code derrière

Labels.xml

<?xml version="1.0" encoding="utf-8" ?> 
<Labels> 
    <btnOne Label="Button1"/> 
    <btnTwo Label="Button2"/> 
</Labels> 

MainWindow.xaml

<Window 
     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" x:Class="bindings.MainWindow" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <XmlDataProvider x:Key="XMLLabels" Source="Labels.xml" XPath="Labels"/> 
    </Window.Resources> 
    <Grid> 
     <Button Content="{Binding Source={StaticResource XMLLabels}, XPath=btnOne/@Label}" Height="23" HorizontalAlignment="Left" Margin="12,12,0,276" Name="btnOne" Width="75" /> 
     <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="93,12,0,276" Name="btnTwo" Width="75" Loaded="btnTwo_Loaded" />  
    </Grid> 
</Window> 

MainWindow.xaml.cs

... 
private void btnTwo_Loaded(object sender, RoutedEventArgs e) 
{ 
    String Type = sender.GetType().Name; 
    if (Type == "Button") 
    { 
     Button btn = sender as Button; 
     Binding label = new Binding("XMLBind"); 
     XmlDataProvider xmlLabels = (XmlDataProvider)this.FindResource("XMLLabels"); 
     label.Source = xmlLabels; 
     label.XPath = "btnTwo/@Header"; 
     btn.SetBinding(Button.ContentProperty, label); 
    } 
} 
... 

La liaison au contenu des btnOne fonctionne comme aspecté "Button1". Mais btnTwo est défini sur une chaîne vide. La sortie ne montre aucune erreur.

Merci pour tout conseil.

Répondre

0

ne devrait pas

label.XPath = "btnTwo/@Header"; 

être

label.XPath = "btnTwo/@Label"; 
+0

Merci. Parfois c'est si facile ;-) –

Questions connexes