2011-08-05 20 views
5

このエラーに関する質問がありました。私はいくつかを見つけて読みましたが、正直言って、私は事を理解しませんでした。参照でバインド元が見つかりません。リストビューの問題

データバインドされた2つのリストビューを持つWPFウィンドウがあります。 1つはビジネスオブジェクト(私のカスタムクラス)にバインドされ、もう1つはDictionary<string, string>にバインドされます。すべては、実行時にOKを見ているようだが、私は、出力ウィンドウにエラーを取得しています:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'ListViewItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')

と同じVerticalContentAlignmentのために。

bost ListViewにアイテムが表示されているにもかかわらず、実際にはウィンドウを読み込む際に目立つ遅延が発生します。

このスレッドは、http://social.msdn.microsoft.com/Forums/en/wpf/thread/f3549b2b-5342-41a1-af04-d55e43c48768が見つかりました。両方のリストビューでHorizontalContentAlignmentVerticalContentAlignmentの両方のデフォルト値を指定して、提案されたソリューションを実装しました。それは助けになりませんでした。

  • リストビュー1:ここでは

    はXAMLです

       <ListView Margin="15,50,15,15" Name="lvLanguageCodes" FontSize="13" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"> 
            <ListView.Resources> 
             <Style TargetType="ListViewItem"> 
              <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
              <Setter Property="VerticalContentAlignment" Value="Stretch" /> 
             </Style> 
            </ListView.Resources> 
            <ListView.ItemsPanel> 
             <ItemsPanelTemplate> 
              <UniformGrid Columns="3" /> 
             </ItemsPanelTemplate> 
            </ListView> 
    
  • ListViewの2

         <ListView.Resources> 
              <Style TargetType="ListViewItem"> 
               <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
               <Setter Property="VerticalContentAlignment" Value="Stretch" /> 
               <EventSetter Event="Selected" Handler="lvItemSelected" /> 
              </Style> 
              <Style x:Key="GrayOutMappedColumn" TargetType="{x:Type TextBlock}"> 
               <Style.Triggers> 
                <DataTrigger Binding="{Binding Mapped}" Value="False"> 
                 <Setter Property="TextElement.Foreground" Value="Black" /> 
                </DataTrigger> 
               </Style.Triggers> 
               <Setter Property="TextElement.Foreground" Value="DarkGray" /> 
              </Style> 
             </ListView.Resources> 
             <ListView.GroupStyle> 
              <GroupStyle> 
               <GroupStyle.HeaderTemplate> 
                <DataTemplate> 
                 <Border BorderBrush="LightGray" BorderThickness="0,0,0,1"> 
                  <TextBlock FontSize="12" FontWeight="Bold" Margin="0,10" Text="{Binding Name}" /> 
                 </Border> 
                </DataTemplate> 
               </GroupStyle.HeaderTemplate> 
              </GroupStyle> 
             </ListView.GroupStyle> 
             <ListView.ItemsPanel> 
              <ItemsPanelTemplate> 
               <UniformGrid Columns="4" /> 
              </ItemsPanelTemplate> 
             </ListView.ItemsPanel> 
             <ListView.ItemTemplate> 
              <DataTemplate> 
               <StackPanel ClipToBounds="False" HorizontalAlignment="Stretch" Width="Auto"> 
                <TextBlock HorizontalAlignment="Stretch" Style="{StaticResource GrayOutMappedColumn}" Text="{Binding Path=FriendlyName}" Width="Auto" /> 
               </StackPanel> 
              </DataTemplate> 
             </ListView.ItemTemplate> 
    

データバインディングコード:

lvLanguageCodes.ItemsSource = languages; 
lvLanguageCodes.SelectedValuePath = "Key"; 
lvLanguageCodes.DisplayMemberPath = "Value"; 

2:

lvDataTypes.ItemsSource = AssignDataType.datatypes; 

ここでdatatypesObservableCollection<Gate>です。Gateは、私のビジネスクラスです(INotifyPropertyChangedIComparableを実装し、それ以外は特別なことはありません)。

エラーが発生するのはなぜですか?値を明示的に設定すると、これらの配置プロパティを何かにバインドしようとしているのはなぜですか?

+0

なぜあなたは、スタイルを使用してリストビューのいくつかのプロパティを設定するのですか?これは、共有リソースなどとして有用です。 ListView要素で直接宣言します。 –

+0

あなたのXAMLで 'RelativeSource'バインディングが表示されません。投稿できますか? – Rachel

+0

それは私が探しているものですが、単純にはありません! XAMLに文字列 "relat"(大文字小文字を区別しない)も含まれていません –

答えて

5

デフォルトのItemContainerStyleを上書きする必要があります。だからBlamの答えは半分正しい。ここでは私のものです:

<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem"> 
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/> 
    <Setter Property="OverridesDefaultStyle" Value="True"/> 
    <Setter Property="SnapsToDevicePixels" Value="True"/> 
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
    <Setter Property="VerticalContentAlignment" Value="Stretch"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
    ... 
</Style> 

<ListBox ItemContainerStyle="{StaticResource ListBoxItemStyle}" 
+0

私はあなたが正しいと思っています(それはしばらくしていて、もうそのアプリケーションでは動作しないので、これを確認することはできません)。しかし、私はまだWHYを理解したいと思います。なぜ、ItemContainerStyleをオーバーライドする必要がありますか?それでは、 'ListViewItem'は何ですか? –

+0

ListViewItemのスタイルをインライン化すると、既定のスタイル全体がオーバーライドされません。私の前提は、デフォルトのスタイルのどこかに、VerticalContentAlignmentのRelativeSourceバインディングがあることです(デフォルトのスタイルの例はここを参照してください:http://msdn.microsoft.com/en-us/library/ms788747(v=vs)。 110).aspx)。 – Charlie

+0

説明をありがとう。うーん、私はWPFをプログラマフレンドリーなものとして評価していないでしょう:)地球上のどのような人が、元のエラーメッセージが何を意味するのか、WPFの内部を知らなくても結論に至るはずです!回答が受け入れられました –

1

下記を試してください。あなたの環境ではうまく動作しませんが、これは私のために働く構文です。

<ListView.Resources> 
      <Style TargetType="{x:Type ListViewItem}"> 
       <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
       <Setter Property="VerticalContentAlignment" Value="Stretch" /> 
      </Style> 

データバインディングの問題である可能性があります。試してみることができますか?

<ListView Margin="15,50,15,15" Name="lvLanguageCodes" FontSize="1" 
       PresentationTraceSources.TraceLevel="High" 
       DisplayMemberPath="Value" SelectedValuePath="Key" ItemsSource="Langages"> 
     <ListView.Resources> 
      <Style TargetType="ListViewItem"> 
       <Setter Property="HorizontalContentAlignment" Value="Stretch" /> 
       <Setter Property="VerticalContentAlignment" Value="Stretch" /> 
      </Style> 
     </ListView.Resources> 
    </ListView> 

ListView.ItemsPanelがクローズされていないことがわかります。私はこのコンパイルに驚いています。

+1

同じこと、残念ながら。 –

関連する問題