简体   繁体   中英

Defining UserControl Properties and binding them in windows phone

In my windows phone application, I made a user control that has some text blocks and images. It's something like a control to show posts like Facebook. I want to use this user control in a long list selector, but whenever I try to bind data to it I get no data. Please help me. This is the 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>

And this is the code behind

 public class TestData
{
    private string _TitleText;

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

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

And this is the UserControl xaml code

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

And this is the code behind :

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

You need to use DependencyProperties to be able to bind to an attribute. Rather than re-write the code for you, I will give you a link to a blog by Jerry Nixon, who explains the process well.

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

EDIT:

Code Behind for the Usercontrol would look like.

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

Then in your userControls XAML you would have something like:

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

You can then use the same Binding format in the MainPage.xaml as in the usercontrols XAML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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