このエラーに関する質問がありました。私はいくつかを見つけて読みましたが、正直言って、私は事を理解しませんでした。参照でバインド元が見つかりません。リストビューの問題
データバインドされた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が見つかりました。両方のリストビューでHorizontalContentAlignment
とVerticalContentAlignment
の両方のデフォルト値を指定して、提案されたソリューションを実装しました。それは助けになりませんでした。
リストビュー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;
ここでdatatypes
はObservableCollection<Gate>
です。Gate
は、私のビジネスクラスです(INotifyPropertyChanged
、IComparable
を実装し、それ以外は特別なことはありません)。
エラーが発生するのはなぜですか?値を明示的に設定すると、これらの配置プロパティを何かにバインドしようとしているのはなぜですか?
なぜあなたは、スタイルを使用してリストビューのいくつかのプロパティを設定するのですか?これは、共有リソースなどとして有用です。 ListView要素で直接宣言します。 –
あなたのXAMLで 'RelativeSource'バインディングが表示されません。投稿できますか? – Rachel
それは私が探しているものですが、単純にはありません! XAMLに文字列 "relat"(大文字小文字を区別しない)も含まれていません –