c# - 定义 UserControl 属性并在 Windows Phone 中绑定(bind)它们

标签 c# xaml windows-phone-8 user-controls

在我的 Windows Phone 应用程序中,我制作了一个包含一些文本 block 和图像的用户控件。它类似于显示 Facebook 帖子的控件。我想在长列表选择器中使用此用户控件,但每当我尝试将数据绑定(bind)到它时,我都没有得到任何数据。请帮我。 这是 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 才能绑定(bind)到属性。我不会为您重新编写代码,而是会给您一个指向 Jerry Nixon 的博客的链接,他很好地解释了这个过程。

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

编辑:

Usercontrol 的代码隐藏看起来像。

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 中相同的绑定(bind)格式。

关于c# - 定义 UserControl 属性并在 Windows Phone 中绑定(bind)它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24708040/

相关文章:

html - 当溢出设置为自动或在 WP8 WebBrowser 控件中滚动时弹跳

c++ - Windows Phone 8 的 MP4/h264 解码库

c# - 为 WP8 平台构建原生 OpenSSL 库

c# - 在 DataGridView 中显示 BindingList<(int, int, int)>

c# - ASP.NET Server.HtmlEncode 不会编码 €

c# - Windows 通用应用程序实时加速度计数据图表

c# - BitmapImage 与 ImageBrush 的性能

c# - 'Nancy.Bootstrapper.AppDomainAssemblyTypeScanner' 的类型初始值设定项引发异常

c# - 在 ASP.Net MVC 中删除记录的安全方法

wpf - VisualStateManager VisualGroup 优先级