如何绑定列表框里面WP8用户数据的控制 [英] How to bind data user control inside listbox WP8

查看:115
本文介绍了如何绑定列表框里面WP8用户数据的控制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户控制。它有一个的TextBlock 。我想在其绑定文本

 <用户控件X:类=PhoneApp1.MyUserControl
的DataContext = {绑定的RelativeSource = {自我的RelativeSource}}>
< StackPanel的WIDTH =200HEIGHT =200背景=红>
< TextBlock的文本={结合文字}/>
< / StackPanel的>
< /用户控件>

MyUserControls.xaml.cs

 公共部分类的MyUserControl:用户控件
{
公众的MyUserControl()
{
的InitializeComponent();
}

公共静态只读的DependencyProperty TextProperty =
DependencyProperty.Register(
文本,
typeof运算(字符串),
typeof运算(的MyUserControl),
新PropertyMetadata(NULL));

公共字符串文本
{
{返回(串)的GetValue(TextProperty); }

{
的SetValue(TextProperty,值);
}
}
}

当我绑定到的ListBox 它不能正常工作

 <电网X:NAME =的ContentPanelGrid.Row =1保证金=12,0,12,0> 
< ListBox的X:名称=mainListBox的ItemsSource ={结合}>
< ListBox.ItemTemplate>
<&DataTemplate的GT;
<局部:文本的MyUserControl ={结合文字}保证金=10/>
< / DataTemplate中>
< /ListBox.ItemTemplate>
< /列表框>
< /网格和GT;

和MainPage.xaml中



 公共部分类的MainPage:的PhoneApplicationPage 
{
的ObservableCollection<使用者>名单=新的ObservableCollection<使用者>();

公众的MainPage()
{
的InitializeComponent();
用户的用户=新用户(青);
list.Add(用户);
list.Add(新用户(lfjlkgj));
的DataContext =清单;
}
}

公共类用户
{
公共字符串文本{搞定;组; }

公众用户(字符串名称)
{
文本=名称;
}
}



如何正确绑定到用户控件?谢谢!


解决方案

用户控件 XAML中删除此行



<预类=郎咸平的XML prettyprint-覆盖> 的DataContext ={绑定的RelativeSource = {的RelativeSource自}}

在绑定控件的XAML将正确绑定到定义依赖属性。你不需要做任何额外的费用。



如果您有名称冲突与您的用户控制的其他数据源,你可以添加的ElementName =用户控件您绑定。


I have a user control. It has a TextBlock. I want to bind Text in it

<UserControl x:Class="PhoneApp1.MyUserControl"
             DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <StackPanel Width="200" Height="200" Background="Red">
        <TextBlock Text="{Binding Text}" />
    </StackPanel>
</UserControl>

In MyUserControls.xaml.cs

public partial class MyUserControl : UserControl
{
    public MyUserControl()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty TextProperty = 
        DependencyProperty.Register(
            "Text", 
            typeof(string), 
            typeof(MyUserControl), 
            new PropertyMetadata(null));

    public string Text
    {
        get { return (string)GetValue(TextProperty); }
        set
        {
            SetValue(TextProperty, value);
        }
    }
}

When I bind to ListBox it doesn't work correctly

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
    <ListBox x:Name="mainListBox" ItemsSource="{Binding}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <local:MyUserControl Text="{Binding Text}" Margin="5"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

and MainPage.xaml

public partial class MainPage : PhoneApplicationPage
{
    ObservableCollection<User> list = new ObservableCollection<User>();

    public MainPage()
    {
        InitializeComponent();
        User user = new User("Thanh");
        list.Add(user);
        list.Add(new User("lfjlkgj"));
        DataContext = list;
    }
}

public class User
{
    public string Text { get; set; }

    public User(string name)
    {
        Text = name;
    }
}

How to bind to user controls correctly? Thanks!!!

解决方案

Remove this line from your UserControl Xaml

DataContext="{Binding RelativeSource={RelativeSource Self}}"

Binding in the control's xaml will correctly bind to the defined dependency properties. You don't need to do anything extra.

If you have name conflicts with other data sources in your user control you can add ElementName=UserControl to your binding.

这篇关于如何绑定列表框里面WP8用户数据的控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆