c# - Windows Phone 应用程序中用户控件的绑定(bind)属性

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

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>

此时,一切正常,MVVM 和绑定(bind)都正常。

我想将此 XAML 移动到 UserControl 中并从中绑定(bind)这些属性。而且,我想以这种方式进行:

<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 中使用用户控件:

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

但是出了点问题,我不知道是什么。我想它必须使用不正确的绑定(bind)来做某事......因为在我的应用程序中,一个页面加载了我在这个问题中提出的这个 XAML,并且应用程序没有崩溃。然后用户必须导航到另一个页面,其中添加了一些数据并且 ViewModel 将有一些数据要显示,所以当它返回到主页时,这一次,它只是崩溃了......(让我到 App.xaml.cs 中的 Application_UnhandledException 方法中断调试器。

其他研究

我已经设法找到异常,它似乎...

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'

我仍然对如何解决这个问题感到困惑......

欢迎提出任何建议来帮助我找出问题所在。谢谢!

最佳答案

为了能够绑定(bind)到属性,它必须是 dependency property .以下是需要修改 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); }
        }
....
}

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

关于c# - Windows Phone 应用程序中用户控件的绑定(bind)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19072197/

相关文章:

c# - JavaScript 中的字符串.Format ("{0:n0}")

.net - 突出显示被拖动的 TreeView 项目

c# - Xamarin.Forms.Xaml.XamlParseException

asp.net - 如何填充asp :GridView from code?

css - 如何将 bind-attr 类附加到 Ember 中的初始类?

wpf - WPF 转换器可以访问它所绑定(bind)的控件吗?

c# - StructureMap:在基类中注入(inject)原始属性

c# - 防止 WPF 中的内存泄漏

c# - 未连接控制台时,Console.WriteLine 是否会在 WinForms 应用程序中占用任何时间?

xaml - 更改 ListView 项目的颜色并删除项目 UWP