【问题标题】:Defining UserControl Properties and binding them in windows phone定义 UserControl 属性并在 windows phone 中绑定它们
【发布时间】:2023-03-16 05:10:02
【问题描述】:

在我的 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; }}

【问题讨论】:

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


    【解决方案1】:

    您需要使用 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 中相同的绑定格式。

    【讨论】:

    • 感谢您的回复。我已经在博客上找到了 Jerry Nixon 的文章,并尝试编写该示例代码。现在,我如何在 MainPage.xaml 中绑定用户控件?如果你有它的示例代码,请发给我
    • 考虑到您已经找到 Jerry 的文章,我认为您的示例代码应该至少包含一些依赖属性?
    • 确定,只有一个问题。如何在我的用户控件中对图像执行此操作?我的意思是将图像的来源绑定到一个 url ?
    • 在我上面给出的示例中使用字符串而不是双精度。然后,当您在主页中设置属性时,使用 url 转换为字符串进行设置
    • 请您现在选择我的答案是否正确
    猜你喜欢
    • 2010-12-04
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    • 1970-01-01
    相关资源
    最近更新 更多