简体   繁体   English

Windows Phone应用程序中用户控件的绑定属性

[英]Binding properties of User Control in Windows Phone Application

In a LongListSelector , I have multiple items shown, according to the following DataTemplate : LongListSelector中,根据以下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>

At this point, everything works fine, the MVVM and bindings are OK. 至此,一切正常,MVVM和绑定都正常。

I wanted to move this XAML into an UserControl and bind those properties from it. 我想将此XAML移至UserControl并从中绑定那些属性。 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>

And this is the C# class : 这是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();
    }
}

I have thought to use the user control in XAML in this way : 我曾考虑过以这种方式在XAML中使用用户控件:

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

But something went wrong and I can't figure out what. 但是出了点问题,我不知道是什么。 I guess it has to do something with an incorrect binding... because in my application, a page is loaded with this XAML I presented in this issue and the app doesn't crash. 我猜想它必须使用不正确的绑定来做某件事...因为在我的应用程序中,页面加载了此问题中介绍的XAML,并且该应用程序不会崩溃。 Then the user has to navigate to another page, where some data is added and the ViewModel will have some data to show, so when it returns to the main page, this time, it simply crashes... (gets me to Application_UnhandledException method in App.xaml.cs to break the debugger. 然后,用户必须导航到另一个页面,在该页面中添加了一些数据,并且ViewModel将显示一些数据,因此,当这一次它返回主页时,它只是崩溃了...(使我进入到Application_UnhandledException方法App.xaml.cs破坏调试器。

Additional research 附加研究

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! 谢谢!

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: 这是需要修改title属性的方式:

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

You will need to do the same thing with the LastModifiedDate property. 您将需要使用LastModifiedDate属性执行相同的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM