0

我有 3 DependencyProperty 的自定义控件:

<UserControl x:Class="WpfDemoApp.NewsCard" [...]>
<Border BorderThickness="10" CornerRadius="10" BorderBrush="Wheat" Background="Wheat" Margin="3">
    <StackPanel Background="Wheat">
        <TextBlock Text="{Binding Date}" TextAlignment="Left"/>
        <TextBlock Text="{Binding Title}" FontWeight="Bold" TextAlignment="Left" TextWrapping="Wrap" Margin="0, 3, 0, 3"/>
        <TextBlock Text="Description:"  FontWeight="Bold" />
        <TextBlock Text="{Binding Text}" TextAlignment="Justify" TextWrapping="Wrap" TextTrimming="WordEllipsis"/>
    </StackPanel>
</Border>
</UserControl>

在 C# 代码中:

public partial class NewsCard : UserControl
{
    public NewsCard()
    {
        InitializeComponent();
        DataContext = this;
    }

    public string Title
    {
        get { return (string)GetValue(TitleProperty); }
        set { SetValue(TitleProperty, value); }
    }
    public static readonly DependencyProperty TitleProperty =
        DependencyProperty.Register("Title", typeof(string), typeof(NewsCard), new UIPropertyMetadata("<title>"));

    public string Date
    {
        get { return (string)GetValue(DateProperty); }
        set { SetValue(DateProperty, value); }
    }
    public static readonly DependencyProperty DateProperty =
        DependencyProperty.Register("Date", typeof(string), typeof(NewsCard), new UIPropertyMetadata("01.01.1970"));

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set { SetValue(TextProperty, value); }
    }
    public static readonly DependencyProperty TextProperty =
        DependencyProperty.Register("Text", typeof(string), typeof(NewsCard), new UIPropertyMetadata("<text>"));   
}

我正在使用以下 XML 数据提供程序:

<XmlDataProvider x:Key="RssData" XPath="//item" Source="http://www.engadget.com/rss.xml"/>

并尝试在主窗口 XAML 文件中使用它:

<demo:NewsCard Title="{Binding Source=RssData, XPath=title[1]}" Date="{Binding Source=RssData, XPath=pubDate[1]}" Text="{Binding Source=RssData, XPath=description[1]}"/>

所有属性都收到相同的错误消息:

System.Windows.Data Error: 45 : BindingExpression with XPath cannot bind to non-XML object.; XPath='title[1]' BindingExpression:Path=/InnerText; DataItem='String' (HashCode=-696447263); target element is 'NewsCard' (Name=''); target property is 'Title' (type 'String') RssData

当我在 TextBox 控件的 Text 属性中使用完全相同的绑定表达式时,一切正常。当我尝试将它与我的控件一起使用时出了什么问题?非常感谢帮助!

4

2 回答 2

1

不知道为什么它会在 TextBox 上工作,但Source应该将 Binding 指定为StaticResource. 否则源对象只是字符串“RssData”。

Title="{Binding Source={StaticResource RssData}, XPath=title[1]}" 
于 2013-08-06T12:53:30.007 回答
0

终于找到了“恶”的根源。在我的自定义组件类中,Context 被设置为类自实例,这可以防止 XML 数据被设置为组件的上下文。这就是为什么我得到“XPath 无法绑定到非 XML 对象”的原因,上下文指向对象本身,并且由于它不是 XML 数据 - 使用 XPath 会引发错误。

因此,我删除了以下行:

DataContext = this;

从构造函数并在 XAML 文件中设置相对数据绑定:

[...]
<StackPanel Background="Wheat">
    <TextBlock Text="{Binding Date, RelativeSource={RelativeSource AncestorType={x:Type wpfDemoApp:NewsCard}}}" TextAlignment="Left"/>
    <TextBlock Text="{Binding Title, RelativeSource={RelativeSource AncestorType={x:Type wpfDemoApp:NewsCard}}}" FontWeight="Bold" TextAlignment="Left" TextWrapping="Wrap" Margin="0, 3, 0, 3"/>
    <TextBlock Text="Description:"  FontWeight="Bold" />
    <TextBlock Text="{Binding Text, RelativeSource={RelativeSource AncestorType={x:Type wpfDemoApp:NewsCard}}}" TextAlignment="Justify" TextWrapping="Wrap" TextTrimming="WordEllipsis"/>
</StackPanel>

在这些更改之后,组件按预期工作。

所以,简而言之:

  • 删除构造函数中设置的显式上下文
  • 在 XAML 中设置 RelativeSource 绑定
于 2013-08-11T09:51:56.653 回答