【问题标题】:Bind an element in an ItemTemplate to a value outside the ItemSource in Windows Phone 7将 DataTemplate 中的元素绑定到 Windows Phone 7 中 ItemSsource 之外的值
【发布时间】:2011-05-09 20:52:15
【问题描述】:

我有一个自定义的UserControl,它由一个ListBox 和一个DataTemplate 组成。 ListBoxXAML 中获取它的源集,DataTemplate 中的元素从Binding 中获取它的值。

我的 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>

还有代码隐藏:

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;
        }
    }
}

在我的 MainPage.xaml 中,我这样使用它:

<userControls:TracksListBox x:Name="TopTracksListBox"
                TracksList="{Binding ElementName=this, Path=TopTracks}"
                Show="True"/>

我的问题是ListBox 中的CheckBox DataTemplate 不会从Show 获得值。 Grid.Row="0" 中的另一个 CheckBox 得到正确的值......

【问题讨论】:

    标签: data-binding xaml user-controls windows-phone-7 datatemplate


    【解决方案1】:

    这一定是一个错误,没有什么我尝试过的 CheckBox 的 DataContext 总是以 null 或 Track 结束。如果你想解决它,你可以添加它,直到它得到修复

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

    然后在加载后在代码中设置绑定

    private void CheckBox_Loaded(object sender, RoutedEventArgs e)
    {
        CheckBox checkBox = sender as CheckBox;
        Binding isCheckedBinding = new Binding("Show");
        isCheckedBinding.Source = this;
        checkBox.SetBinding(CheckBox.IsCheckedProperty, isCheckedBinding);
    }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 没错。正如我所说,ListBox 上方的复选框是来自Show 的正确值,但是当我将列表绑定到ListBox 时,列表的每个元素中的复选框都没有得到正确的值...
      猜你喜欢
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-02
      • 2019-12-11
      • 2011-12-09
      • 1970-01-01
      • 2013-05-24
      相关资源
      最近更新 更多