数据绑定无法正常工作

时间:2012-01-22 14:56:07

标签: data-binding windows-phone-7.1

我认为这可能是一个错误。我有一个带有5个项目的枢轴控件的页面:

每个PivotItem引用与静态资源相同的模板,并且它们各自具有不同的datacontext(ObservableCollection)。该模板具有ListBox和TextBlock。两者都使用值转换器来实现可见性。基本上它根据集合是否为空来隐藏和显示它们:

<phone:PhoneApplicationPage 
x:Class="PhoneApp2.PivotPage1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:p="clr-namespace:PhoneApp2"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait"  Orientation="Portrait"
shell:SystemTray.IsVisible="True">
<phone:PhoneApplicationPage.Resources>
    <p:CollectionToVisibilityConverter x:Key="CollectionToVisibility" />
    <ControlTemplate x:Key="Template">
        <StackPanel>
            <ListBox ItemsSource="{Binding}" Visibility="{Binding Converter={StaticResource CollectionToVisibility}, ConverterParameter=false}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Text1}" Padding="5,0" />
                            <TextBlock Text="{Binding Text2}" />
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
            <TextBlock Text="Empty" Visibility="{Binding Converter={StaticResource CollectionToVisibility}, ConverterParameter=true}" />
        </StackPanel>

    </ControlTemplate>
</phone:PhoneApplicationPage.Resources>
<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="150" />
    </Grid.RowDefinitions>
    <controls:Pivot Title="MY APPLICATION">
        <controls:PivotItem Header="item1" DataContext="{Binding Model1}" Template="{StaticResource Template}" />
        <controls:PivotItem Header="item2" DataContext="{Binding Model2}" Template="{StaticResource Template}" />
        <controls:PivotItem Header="item3" DataContext="{Binding Model3}" Template="{StaticResource Template}" />
        <controls:PivotItem Header="item4" DataContext="{Binding Model4}" Template="{StaticResource Template}" />
        <controls:PivotItem Header="item5" DataContext="{Binding Model5}" Template="{StaticResource Template}" />
    </controls:Pivot>
    <StackPanel Grid.Row="1">
        <Button Content="Clear Items" Click="Button_Click" />
        <Button Content="Add Items" Click="Button_Click_1" />
    </StackPanel>

</Grid>   

代码背后:

public partial class PivotPage1 : PhoneApplicationPage
{
    public PivotPage1()
    {
        InitializeComponent();
        var vm = new ViewModel();

        vm.Model1 = new ObservableCollection<Model>();
        vm.Model2 = new ObservableCollection<Model>();
        vm.Model3 = new ObservableCollection<Model>();
        vm.Model4 = new ObservableCollection<Model>();
        vm.Model5 = new ObservableCollection<Model>();

        Populate(vm);

        DataContext = vm;
    }

    private void Populate(ViewModel vm)
    {
        vm.Model1.Add(new Model { Text1 = "1", Text2 = "World" });
        vm.Model1.Add(new Model { Text1 = "1", Text2 = "Planet" });

        vm.Model2.Add(new Model { Text1 = "2", Text2 = "World" });
        vm.Model2.Add(new Model { Text1 = "2", Text2 = "Planet" });

        vm.Model3.Add(new Model { Text1 = "3", Text2 = "World" });
        vm.Model3.Add(new Model { Text1 = "3", Text2 = "Planet" });

        vm.Model4.Add(new Model { Text1 = "4", Text2 = "World" });
        vm.Model4.Add(new Model { Text1 = "4", Text2 = "Planet" });

        vm.Model5.Add(new Model { Text1 = "5", Text2 = "World" });
        vm.Model5.Add(new Model { Text1 = "5", Text2 = "Planet" });
    }

    private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        ViewModel vm = DataContext as ViewModel;
        vm.Model1.Clear();
        vm.Model2.Clear();
        vm.Model3.Clear();
        vm.Model4.Clear();
        vm.Model5.Clear();
    }

    private void Button_Click_1(object sender, System.Windows.RoutedEventArgs e)
    {
        ViewModel vm = DataContext as ViewModel;
        Populate(vm);
    }
}

public class ViewModel
{
    public ObservableCollection<Model> Model1 { get; set; }
    public ObservableCollection<Model> Model2 { get; set; }
    public ObservableCollection<Model> Model3 { get; set; }
    public ObservableCollection<Model> Model4 { get; set; }
    public ObservableCollection<Model> Model5 { get; set; }
}

public class Model
{
    public string Text1 { get; set; }
    public string Text2 { get; set; }
}

转换器:

public class CollectionToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        IList list = value as IList;
        bool isEmptyVisible = false;
        if (parameter != null)
            bool.TryParse(parameter.ToString(), out isEmptyVisible);

        if (list == null || list.Count == 0)
            return isEmptyVisible ? Visibility.Visible : Visibility.Collapsed;

        return isEmptyVisible ? Visibility.Collapsed : Visibility.Visible;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

如果你运行这个应用程序并清除并重新填充集合(按下按钮),你会注意到一些透视项目按预期工作,而其他透镜项目没有。

这是一个错误吗?我做错了吗?

谢谢,

1 个答案:

答案 0 :(得分:0)

唯一突出的是你的Model类缺少INotifyPropertyChanged

public class Model : INotifyPropertyChanged
{
    private string m_Text1;
    public string Text1 
    { 
        get { return m_Text1;} 
        set { m_Text1 = value; 
              OnPropertyChanged(new PropertyChangedEventArgs("Text1")); }

    // ...

    #region INotifyPropertyChanged: Shared bit
    public event PropertyChangedEventHandler PropertyChanged;

    private void OnPropertyChanged(PropertyChangedEventArgs e)
    {
    if (PropertyChanged != null)
        PropertyChanged(this, e);
    }
    #endregion

}