Data binding 将ItemTemplate中的元素绑定到Windows Phone 7中ItemSource之外的值

Data binding 将ItemTemplate中的元素绑定到Windows Phone 7中ItemSource之外的值,data-binding,xaml,user-controls,windows-phone-7,datatemplate,Data Binding,Xaml,User Controls,Windows Phone 7,Datatemplate,我有一个自定义的UserControl,它由一个ListBox和一个DataTemplate组成。 ListBox在XAML中获取其源代码集,DataTemplate中的元素从绑定中获取其值 我的UserControl XAML如下所示: <UserControl x:Class="Test.UserControls.TracksListBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

我有一个自定义的
UserControl
,它由一个
ListBox
和一个
DataTemplate
组成。
ListBox
XAML
中获取其源代码集,
DataTemplate
中的元素从
绑定中获取其值

我的UserControl XAML如下所示:

<UserControl x:Class="Test.UserControls.TracksListBox"
    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"
    xmlns:converters="clr-namespace:Test.Converters"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
    mc:Ignorable="d"
    d:DesignHeight="480" d:DesignWidth="480"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    Name="this">

<UserControl.Resources>
    <converters:BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter"/>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <CheckBox Grid.Row="0" IsChecked="{Binding Show}"/>
    <ListBox Grid.Row="1" Name="List">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel VerticalAlignment="Top"
                            Margin="0 5 0 5">
                    <TextBlock Text="{Binding Title}"/>
                    <CheckBox IsChecked="{Binding ElementName=this, Path=Show}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>
<userControls:TracksListBox x:Name="TopTracksListBox"
                TracksList="{Binding ElementName=this, Path=TopTracks}"
                Show="True"/>

以及背后的代码:

namespace Test.UserControls
{
    public partial class TracksListBox : UserControl
    {
        public static readonly DependencyProperty TracksListProperty =
            DependencyProperty.Register("TracksList",
            typeof(List<Track>),
            typeof(TracksListBox),
            new PropertyMetadata(null, OnTracksListChanged));

        public static readonly DependencyProperty ShowProperty =
            DependencyProperty.Register("Show",
            typeof(bool),
            typeof(TracksListBox),
            new PropertyMetadata(false));

        public TracksListBox()
        {
            InitializeComponent();
        }

        public List<Track> TracksList
        {
            get
            {
                return (List<Track>)GetValue(TracksListProperty);
            }
            set
            {
                SetValue(TracksListProperty, value);
            }
        }

        public bool Show
        {
            get
            {
                return (bool)GetValue(ShowProperty);
            }
            set
            {
                SetValue(ShowProperty, value);
            }
        }

        private static void OnTracksListChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            (obj as TracksListBox).OnTracksListChanged((List<Track>)args.OldValue, (List<Track>)args.NewValue);
        }

        protected virtual void OnTracksListChanged(List<Track> oldValue, List<Track> newValue)
        {
            List.ItemsSource = newValue;
        }
    }
}
namespace Test.UserControls
{
公共部分类TracksListBox:UserControl
{
public static readonly dependencProperty TracksListProperty=
DependencyProperty.Register(“TracksList”,
类型(列表),
类型(轨道箱),
新的PropertyMetadata(null,OnTracksListChanged));
公共静态只读从属属性ShowProperty=
DependencyProperty.Register(“显示”,
类型(bool),
类型(轨道箱),
新属性元数据(假));
公共轨道机顶盒()
{
初始化组件();
}
公共列表轨道列表
{
收到
{
返回(列表)GetValue(TracksListProperty);
}
设置
{
设置值(TracksListProperty,值);
}
}
公演
{
收到
{
返回(bool)GetValue(ShowProperty);
}
设置
{
设置值(ShowProperty,value);
}
}
私有静态无效OnTracksListChanged(DependencyObject对象,DependencyPropertyChangedEventArgs参数)
{
(对象作为TracksListBox.OnTracksListChanged((列表)args.OldValue,(列表)args.NewValue);
}
受保护的虚拟无效OnTracksListChanged(列出旧值、列出新值)
{
List.ItemsSource=newValue;
}
}
}
在我的MainPage.xaml中,我这样使用它:

<UserControl x:Class="Test.UserControls.TracksListBox"
    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"
    xmlns:converters="clr-namespace:Test.Converters"
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit"
    mc:Ignorable="d"
    d:DesignHeight="480" d:DesignWidth="480"
    DataContext="{Binding RelativeSource={RelativeSource Self}}"
    Name="this">

<UserControl.Resources>
    <converters:BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter"/>
</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <CheckBox Grid.Row="0" IsChecked="{Binding Show}"/>
    <ListBox Grid.Row="1" Name="List">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel VerticalAlignment="Top"
                            Margin="0 5 0 5">
                    <TextBlock Text="{Binding Title}"/>
                    <CheckBox IsChecked="{Binding ElementName=this, Path=Show}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>
<userControls:TracksListBox x:Name="TopTracksListBox"
                TracksList="{Binding ElementName=this, Path=TopTracks}"
                Show="True"/>


我这里的问题是
列表框中的
复选框
数据模板
无法从
显示中获取值。但是,
Grid.Row=“0”
中的另一个
复选框将获得正确的值。。。如何从
列表框的
数据模板中绑定
用户控件中的值?

我将您的代码放入应用程序中的用户控件中,将
Track
更改为
string
,并且没有将任何内容绑定到列表,但我仍然看到显示了一个复选框,并且将
Show
绑定到IsChecked对我有效。

这一定是一个错误,无论我尝试了什么,复选框的DataContext总是以空值或一个轨迹结束。如果你想解决这个问题,你可以添加这个,直到这个问题得到解决

<ListBox.ItemTemplate>
    <DataTemplate>
        <StackPanel VerticalAlignment="Top" 
                Margin="0 5 0 5">
            <TextBlock Text="{Binding Title}"/>
            <CheckBox Loaded="CheckBox_Loaded"/>
        </StackPanel>
    </DataTemplate>
</ListBox.ItemTemplate>

没错。正如我所说,
ListBox
get上面的复选框是
Show
中的正确值,但是当我将列表绑定到
ListBox
时,列表中每个元素中的复选框都没有得到正确的值。。。