1

我在将带有 XPath 的 XMLDataprovider 绑定从 Xaml 移动到代码后面时遇到问题。

标签.xml

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

主窗口.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> 

主窗口.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);
    }
}
...

与 btnOne 内容的绑定作为方面的“Button1”工作。但 btnTwo 设置为空字符串。输出显示没有错误。

感谢您的任何建议。

4

1 回答 1

0

不应该

label.XPath = "btnTwo/@Header";

label.XPath = "btnTwo/@Label";
于 2010-06-09T10:50:42.450 回答