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