繁体   English   中英

定义UserControl属性并将其绑定在Windows Phone中

[英]Defining UserControl Properties and binding them in windows phone

在Windows Phone应用程序中,我制作了一个带有一些文本块和图像的用户控件。 它就像控件一样显示诸如Facebook的帖子。 我想在长列表选择器中使用此用户控件,但是每当我尝试将数据绑定到它时,都不会获得数据。 请帮我。 这是MainPage.xaml

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <phone:LongListSelector Name="myLLS">
            <phone:LongListSelector.ItemTemplate>
                <DataTemplate>
                    <local:Posts TitleText={Binding TitleText}>

                    </local:Posts>
                </DataTemplate>
            </phone:LongListSelector.ItemTemplate>
        </phone:LongListSelector>
    </Grid>

这是背后的代码

 public class TestData
{
    private string _TitleText;

    public string TitleText
    {
        get { return _TitleText; }
        set { _TitleText = value; }
    }

    public TestData(string Text)
    {
        This.TitleText = Text; 
    }
}

这是UserControl xaml代码

<UserControl x:Class="BindingUserControlTest.TestBind"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}" Height="125.889" Width="227.974">

<Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}">
    <TextBlock x:Name="lblTitle" HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top"/>
    <TextBlock x:Name="lblDescription" HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Margin="0,32,0,0" Width="218" Height="84"/>

</Grid>

这是背后的代码:

   private string _ShownTitle;
   public string ShownTitle { get { return _ShownTitle; } set { _ShownTitle = value; }}

您需要使用DependencyProperties才能绑定到属性。 我不会为您重新编写代码,而是为您提供Jerry Nixon博客的链接,他很好地解释了该过程。

http://blog.jerrynixon.com/2013/07/solved-two-way-binding-inside-user.html

编辑:

用户控件的代码隐藏看起来像。

public sealed partial class ExampleControl : UserControl
{

    public static readonly DependencyProperty exampleProperty = DependencyProperty.Register("ExampleData", typeof(Double), typeof(NutritionLabelControl), null);

    public ExampleControl()
    {
        InitializeComponent();
        (this.Content as FrameworkElement).DataContext = this;
    }

    public Double ExampleData
    {
        get { return (Double)GetValue(exampleProperty); }
        set
        {
            SetValue(exampleProperty, value);
        }
    }
}

然后,在您的userControls XAML中,您将得到以下内容:

<UserControl> 
    <Grid x:Name="LayoutRoot">
        <TextBlock Text="{Binding ExampleData}" />
    </Grid>
</UserControl>

然后,您可以在MainPage.xaml中使用与用户控件XAML中相同的绑定格式。

暂无
暂无

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

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