2011-03-09 5 views
0

私のアプリケーションでは、現在非常によく似た2つのリストボックスが機能しています。問題は、一方が適切に動作し、他方が正しく動作しないことです。問題を表示する1は、このいずれかになります。多角形を含むSilverlightリストボックス - スクロール時の例外と失敗

<ListBox Grid.Row="2" Margin="0,35,10,5" Name="shapeEncodingListBox" HorizontalAlignment="Right" Width="225" Style="{StaticResource EncodingListBoxStyle}" BindingValidationError="shapeEncodingListBox_BindingValidationError"> 
      <toolkit:ContextMenuService.ContextMenu> 
       <toolkit:ContextMenu> 
        <toolkit:MenuItem Header="Show Items" Click="MenuItem_Click" /> 
        <toolkit:MenuItem Header="Hide Items" Click="MenuItem_Click_1"/> 
        <toolkit:Separator/> 
        <toolkit:MenuItem Header="Isolate Items" Click="MenuItem_Click_2" /> 
        <toolkit:Separator/> 
        <toolkit:MenuItem Header="Select Items" Click="MenuItem_Click_3" /> 
       </toolkit:ContextMenu> 
      </toolkit:ContextMenuService.ContextMenu> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal" Style="{StaticResource EncodingListBoxItemPanelStyle}"> 
         <Polygon Points="{Binding Shape}" Style="{StaticResource EncodingListBoxItemShapeIndicatorStyle}"/> 
         <TextBlock x:Name="encodeShapePanelLegend" Text="{Binding Name}" Style="{StaticResource EncodingListBoxItemTextStyle}"/> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

このListboxsのItemsSourceは、このクラスのオブジェクトののObservableCollectionにバインドされた2つのリストボックス間の差が働い1であることである

public class ShapeEncoding 
{ 
    private string name; 

    public string Name 
    { 
     get { return name; } 
     set { name = value; } 
    } 
    private PointCollection shape; 

    public PointCollection Shape 
    { 
     get { return shape; } 
     set { shape = value;} 
    } 

    private int index; 

    public int Index 
    { 
     get { return index; } 
     set { index = value; } 
    } 

    public ShapeEncoding(string n, Polygon p, int i) 
    { 
     name = n; 
     shape = new PointCollection(); 
     foreach (Point pt in p.Points) 
     { 
      shape.Add(new Point(pt.X, pt.Y)); 
     } 
     index = i; 
    } 
} 

Color-Propertyにバインドされ、誤ったものはPointCollection-Propertyにバインドされます。

ListBoxは、PointCollectionを正しく初期化し、ポリゴンは意図したとおりに表示されます。ただし、ListBoxをスクロールダウンしてから再び上に移動すると、以前に表示されなかったアイテムは表示されず、表示されない各アイテムに対してエラーが生成されます。 エラーとスタックトレースは次のとおりです。

値が予期された範囲内にありません。 MS.Internal.XcpImports.SetValue(IManagedPeerBaseのDOH、たDependencyPropertyプロパティでMS.Internal.XcpImports.SetValue(IManagedPeerBase OBJ、たDependencyPropertyプロパティ、のDependencyObjectのDOH) でMS.Internal.XcpImports.CheckHResult(UInt32型のHR) で

System.Windows.DependencyObject.UpdateEffectiveValueでSystem.Windows.DependencyObject.SetObjectValueToCore(DPたDependencyProperty、OBJECT>値)で、オブジェクトobj) System.Windows.DependencyObject.SetEffectiveValueで (たDependencyPropertyプロパティ、EffectiveValueEntry & newEntry、newValueにオブジェクト) (DependencyPropertyプロパティ、EffectiveValueEntry oldEntry、EffectiveValueEntry & newEntry、ValueOperationオペレーションSystem.Windows.Data.BindingExpressionでSystem.Windows.Data.BindingExpression.SourceAcquiredでSystem.Windows.Data.BindingExpression.SendDataToTargetでSystem.Windows.DependencyObject.RefreshExpression(たDependencyProperty DP) (AT N) ) () System.Windows.FrameworkElement.OnDataContextChanged(DataContextChangedEventArgs e)にSystem.Windows.Data.BindingExpression.DataContextChanged(オブジェクト送信者、DataContextChangedEventArgs E) で.System.Windows.IDataContextChangedListener.OnDataCont> extChanged(オブジェクト送信者、DataContextChangedEventArgs E) でSystem.Windows.FrameworkElement.NotifyDataContextChanged(DataContextChangedEventArgs e) でSystem.Windows.FrameworkElement.NotifyDataContextChanged(DataContextChangedEventArgs e)MS.Internal.FrameworkCallbacks.ManagedPeerTreeUpdateでSystem.Windows.DependencyObject.UpdateTreeParent(IManagedPeer oldParent、IManagedPeer新規の親、ブールbIsNewParentAlive、ブールkeepReferenceToParent) でSystem.Windows.FrameworkElement.OnTreeParentUpdated(新規の親のDependencyObject、ブールbIsNewParentAlive) (のIntPtr oldParentElementで、のIntPtr> parentElement、のIntPtr childElement、バイトbIsParentAlive、バイトbKeepReferenceToParent、バイト> bCanCreateParent)

私は何が起こっているかのように疑いを持っています。このアプリケーションを作成する過程で、SilverlightのPointCollectionsがPolygons間で共有できないことを認識しました。リストボックスが視野のないアイテムをフェッチしているときに、同じPointCollectionを持つリスト内に新しいアイテムを作成しようとすると、エラーと失敗が発生する可能性はありますか?

これが当てはまる場合、私はそれを解決する最良の方法が何であるかはわかりません。それは、最初の場所で項目をフェッチすることからListBoxを維持する方法はありますか?

答えて

0

まあ答えは非常に簡単です...ない「バインド」を行うと、PresentationFrameworkCollection<T>から継承されたデータとして何を使用しません...

ロジックは単純です - データバインディングは、データへの「バインド」に持っています...

代わりのPointCollection使用List<Points>を使用し、「コンバータ」

public class PointsToPointsCollectionsConverter: IValueConverter 
{ 

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     var points = value as List<Point>; 

     if (points != null) 
     { 
      var pc = new PointCollection(); 
      foreach (var point in points) 
      { 
       pc.Add(point); 
      } 

      return pc; 
     } 
     else return null; 
    } 

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

PointCollectionにこれらの点を変換し、このようにそれをバインドする...

<UserControl x:Class="SilverlightApplication2.MainPage" 
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" 
mc:Ignorable="d" 
xmlns:local="clr-namespace:SilverlightApplication2" 
d:DesignHeight="300" d:DesignWidth="400"> 
<UserControl.Resources> 
    <local:PointsToPointsCollectionsConverter x:Key="ptpc"/> 
</UserControl.Resources> 

<Grid x:Name="LayoutRoot" Background="White"> 
    <ListBox x:Name="testListBox1" Width="100" Height="100"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel> 
        <Polygon Fill="Red" 
Points="{Binding Points, Converter={StaticResource ptpc}}"/> 
        <TextBlock Text="{Binding Name}"/> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

</Grid> 

だけ覚えておいてください。 PresentationFrameworkCollection<T>から継承されたエブリシングは、Silverlight UIの一部です。データとして使用しないでください。

関連する問題