【问题标题】:How to bind DataTemplate on PhoneApplicationPage.Resources when page is already bound to another class?当页面已经绑定到另一个类时,如何在 PhoneApplicationPage.Resources 上绑定 DataTemplate?
【发布时间】:2013-02-07 10:56:09
【问题描述】:

我已根据反馈编辑问题以包含更多信息。

在我的 SettingsPage.xaml 中有

   <toolkit:ListPicker x:Name="lpkCountry" 
ItemTemplate="{Binding Source={StaticResource lpkItemTemplate}}" 
FullModeItemTemplate="{Binding Source={StaticResource lpkFullItemTemplate}}" 
Header="your country" CacheMode="BitmapCache" 
HorizontalAlignment="Left" VerticalAlignment="Top" Width="280" Height="82" />

我正在尝试将 ListPicker DataTemplate 绑定到放置在 App.xaml 页面中的以下 XAML

<Application.Resources>
    <DataTemplate x:Name="lpkItemTemplate">
        <TextBlock Text="{Binding Country}" />
    </DataTemplate>
    <DataTemplate x:Name="lpkFullItemTemplate">
        <TextBlock Margin="16 0 0 0" Text="{Binding Country}" />
    </DataTemplate>
 </Application.Resources>

SettingsPage.xaml.cs 将 Country 定义为公共静态字符串数组:

namespace myspace
{
    public partial class SettingsPage : PhoneApplicationPage
    {
        public static String[] Country = {
                            "Afghanistan",
                            "Åland Islands",
                            "Albania",
                            "Algeria",
                            "American Samoa",
                            "Andorra",
                            "Angola"}
                            ......;

我还在 SettingsPage.xaml.cs 上定义了另一个对象的数据上下文。

    public SettingsPage()
    {
        InitializeComponent();
         this.lpkCountry.SelectionChanged += new  SelectionChangedEventHandler(lpkCountry_SelectionChanged);
        this.lpkCountry.ItemsSource = Country;
        settings = new AppSettings();
        this.DataContext = settings;
    }

但是在运行时,当我转到 SettingsPage 时,我会遇到很多此类错误

System.Windows.Data 错误:BindingExpression 路径错误:在“阿富汗”“System.String”上找不到“Country”属性 (HashCode=-2039466552)。 BindingExpression: Path='Country' DataItem='Afghanistan' (HashCode=-2039466552);目标元素是'System.Windows.Controls.TextBlock'(名称='');目标属性是“文本”(类型“System.String”)..

我了解到目标元素和目标属性之间存在冲突,请问如何解决?

【问题讨论】:

    标签: c# xaml data-binding windows-phone-8


    【解决方案1】:

    一个明显的错误是RelativeSource={RelativeSource Self}。这意味着您尝试绑定到同一个对象,例如 TextBox 或 ListPicker。在这种情况下运行时绝对正确:'lpkItemTemplate' property not found on 'Microsoft.Phone.Controls.ListPicker'

    我想Source={StaticResource lpkItemTemplate} 之类的东西会有所帮助,如果您在 App.xaml 中的某个位置定义您的数据模板,在 &lt;Application.Resources&gt; 部分。

    编辑: 添加更多代码后。您的项目源是一个字符串数组,因此在数据模板中您应该使用以下绑定:

    <TextBox Text={Binding} />
    

    【讨论】:

    • 感谢您的回复。现在的问题是 listpicker 的 text 属性绑定到通过代码定义的类的公共属性。即:&lt;TextBlock Text="{Binding Country}" /&gt;。如果我将数据模板放在 App.xaml 中,正确的使用路径是什么?
    • 数据模板的放置位置无关紧要,因为它只在运行时应用。你应该保持相同的路径,&lt;TextBlock Text={Binding Country} /&gt;
    • 我问的原因是因为我得到了所有国家/地区列表的错误,例如System.Windows.Data Error: BindingExpression path error: 'Country' property not found on 'Zimbabwe' 'System.String' (HashCode=993922277). BindingExpression: Path='Country' DataItem='Zimbabwe' (HashCode=993922277); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..
    • 您的错误表明系统无法在 string 类型上找到 Country 属性,这并不奇怪,真的。看来您的约束性安排有问题。如果 string ("Zimbabwe") 是运行时尝试搜索属性的对象,您应该能够编写如下绑定表达式:&lt;TextBlock Text={Binding} /&gt;。试试看它是否有效。通常,如果不查看所有代码就很难修复绑定,所以如果您仍然无法实现您想要的,请发布更多代码。
    • 我已经编辑了我的问题。我明白它找不到什么,这并不奇怪。这就是为什么我要问如何正确找到它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 2022-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多