结合在Windows Phone应用程序的用户控件的属性 [英] Binding properties of User Control in Windows Phone Application

查看:82
本文介绍了结合在Windows Phone应用程序的用户控件的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LongListSelector ,我已经展示了多个项目,按照下列的DataTemplate

In a LongListSelector, I have multiple items shown, according to the following DataTemplate :

<TextBlock Text="{Binding Subject}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}" />
<StackPanel Orientation="Horizontal">
    <TextBlock Text="Last modified :" Margin="15, 0, 5, 0" Foreground="LightGray" Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock Text="{Binding LastModified}" Foreground="#989696" Style="{StaticResource PhoneTextNormalStyle}"/>
 </StackPanel>

在这一点上,一切工作正常,在MVVM和绑定都OK了。

At this point, everything works fine, the MVVM and bindings are OK.

我想这个XAML移动到用户控件和绑定的属性。而且,我还以为用这种方式进行:

I wanted to move this XAML into an UserControl and bind those properties from it. And, I have thought to proceed in this way :

<UserControl x:Class="..."
    xmlns=" ... "
    Foreground="{StaticResource PhoneForegroundBrush}"
    d:DesignHeight="100" d:DesignWidth="480">

    <StackPanel x:Name="LayoutRoot" Background="Transparent">
        <TextBlock x:Name="TitleTextBlock"  Style="{StaticResource PhoneTextExtraLargeStyle}" />
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="Last modified :" Margin="15, 0, 5, 0" Foreground="LightGray" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="LastModifiedDateTextBlock" Foreground="#989696" Style="{StaticResource PhoneTextNormalStyle}"/>
        </StackPanel>
    </StackPanel>
</UserControl>

这是C#类:

public partial class LongListSelectorItemControl
{
    private DateTime _lastModifiedDate;

    public string Title
    {
        get
        {
            return TitleTextBlock.Text;
        }
        set
        {
            TitleTextBlock.Text = value;
        }
    }

    public DateTime LastModifiedDate
    {
        get
        {
            return _lastModifiedDate;
        }
        set
        {
            LastModifiedDateTextBlock.Text = value.ToString(CultureInfo.InvariantCulture);
            _lastModifiedDate = value;
        }
    }

    public LongListSelectorItemControl()
    {
        InitializeComponent();

        _lastModifiedDate = new DateTime();
    }
}

我也曾想过以这种方式使用用户控件在XAML:

I have thought to use the user control in XAML in this way :

<userControls:LongListSelectorItemControl Title="{Binding Subject}" LastModifiedDate="{Binding LastModified}"/>

但出事了,我想不出什么。我猜它做一些使用不正确的结合......因为我的应用程序,页面加载在这个问题上psented这个XAML我$ P $和应用程序不会崩溃。然后,用户导航到另一个页面,其中一些数据,并将该视图模型会有一些数据显示,所以当它返回到主网页,这时候,它只是崩溃......(让我到 Application_UnhandledException 方法 App.xaml.cs 打破调试器。

其他研究

我已经设法追查例外,它似乎...

I've managed to track down the exception and it seems...

MS.Internal.WrappedException: Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.String'. ---> System.ArgumentException: Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.String'

我仍然困惑于如何解决这一问题...

I am still confused on how to fix this...

任何建议,欢迎帮我进搞清楚什么是错的。谢谢!

Any suggestions are welcome to aid me into figuring out what's wrong. Thanks!

推荐答案

要能够绑定到一个属性,它需要一个的依赖属性。下面是如何title属性需要修改:

To be able to bind to a property, it need to be a dependency property. Here is how the title property need to be modified:

public partial class LongListSelectorItemControl
{


   public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(LongListSelectorItemControl), new PropertyMetadata(default(string), TitlePropertyChanged));

        private static void TitlePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            LongListSelectorItemControl myControl=d as LongListSelectorItemControl;
            myControl.TitleTextBlock.Text = e.NewValue as string;
        }

        public string Title
        {
            get { return (string) GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }
....
}

您需要做同样的事情与LastModifiedDate属性。

You will need to do the same thing with the LastModifiedDate property.

这篇关于结合在Windows Phone应用程序的用户控件的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆