2012-01-22 85 views
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(); 
    } 
} 

如果運行這個應用程序,並清除和重新填充集合(按下按鈕),你會注意到一些數據透視項目按預期工作,其他則不需要。

這是一個錯誤?難道我做錯了什麼?

感謝,

+0

你怎麼樣懶得告訴*如何*如預期,而不是期望他們不工作人們來運行你的代碼。上面的代碼沒有錯,所以問題顯然在別處。從上面的問題 –

+0

@Claus:「如果你運行這個程序,並明確和重新填充集合(按下按鈕),你會發現,一些樞紐項目按預期工作,以及其他不」。更具體地說,一些重要項目按照預期重新綁定,而其他項目保持不變。代碼已添加,因此它可以在本地運行,其他可能會複製該問題。 –

+0

@JonasStawski哪些項目被更新,哪些不是?你有一個完整的repro? –

回答

0

那伸出的唯一的事情是沒有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 

} 
+0

的綁定屬性,模型1,模型2等是那些改變。這些是ObservableCollection,它具有內置的屬性更改機制。 –