Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Pesky binding problem

$
0
0

Is it just me or are binding problems with WPF just not very well supported by VS (2010)?

I have the following error displayed (about one for each item in the listbox (shown below)):

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 'ListBoxItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=VerticalContentAlignment; DataItem=null; target element is 'ListBoxItem' (Name=''); target property is 'VerticalContentAlignment' (type 'VerticalAlignment')

The XAML for the window which is giving the problem is:

<Window x:Class="ArtistSearch"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:NewMediaPlayer"
        Background="Transparent"  
        AllowsTransparency="True" 
        WindowStyle="None" 
        WindowStartupLocation="CenterScreen"
        MouseLeftButtonDown="Window_MouseLeftButtonDown"
        Title="ArtistSearch" Height="500" Width="600" Loaded="Window_Loaded"><Window.Resources><local:ArtistNameVisibilty x:Key="anv"></local:ArtistNameVisibilty><local:FontConverter x:Key="fontSiz"></local:FontConverter><Style x:Key="NoButtonBorder" TargetType="{x:Type Button}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type Button}"><ContentPresenter                  
                            Margin="{TemplateBinding Padding}"                  
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"                  
                            ContentTemplate="{TemplateBinding ContentTemplate}"                  
                            RecognizesAccessKey="True"                  
                            Content="{TemplateBinding Content}" /></ControlTemplate></Setter.Value></Setter></Style></Window.Resources><Border CornerRadius="30" Background="Black"><Grid x:Name="theTopGrid" Background="Transparent"><Grid.RowDefinitions><RowDefinition Height="40"></RowDefinition><RowDefinition Height="*"></RowDefinition></Grid.RowDefinitions><StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Margin="0,0,10,0"><Button Style="{StaticResource NoButtonBorder}" x:Name="minButton" Width="22" Background="{x:Null}" Click="minButton_Click"><Image Source="/NewMediaPlayer;component/Images/Minimize.png"></Image></Button><Button Style="{StaticResource NoButtonBorder}" x:Name="closeButton" Width="22" Background="{x:Null}" Click="closeButton_Click"><Image Source="/NewMediaPlayer;component/Images/Close.png" Name="Image2"></Image></Button></StackPanel><Grid x:Name="artistGrid" Focusable="True" Background="Transparent" Grid.Row="1"
                          PreviewKeyDown="artistGrid_PreviewKeyDown"><Grid.RowDefinitions><RowDefinition Height="50"></RowDefinition><RowDefinition Height="*"></RowDefinition></Grid.RowDefinitions><StackPanel Orientation="Horizontal" Grid.Row="0" ><TextBlock Text="{Binding ArtistFirstChar}" Grid.Row="0" Foreground="Beige" FontSize="48" FontFamily="Algerian"></TextBlock><ComboBox x:Name="GList" ItemsSource="{Binding GenreList}" Height="30" Width="100" Margin="20,0,0,0" 
                                      SelectedIndex="0" SelectionChanged="ComboBox_SelectionChanged" Focusable="False" ></ComboBox></StackPanel><ListBox x:Name="artistNames" Grid.Row="1"  VerticalAlignment="Top"  
                                    Background="Transparent" BorderThickness="0"
                                    ScrollViewer.HorizontalScrollBarVisibility="Disabled"  
                                    ScrollViewer.VerticalScrollBarVisibility="Auto"><ListBox.ItemsPanel><ItemsPanelTemplate><WrapPanel Orientation="Horizontal"/></ItemsPanelTemplate></ListBox.ItemsPanel><ListBox.ItemTemplate><DataTemplate><Button x:Name="artistButton" Style="{StaticResource NoButtonBorder}" Content="{Binding}" Margin="0,0,12,8" Foreground="Beige" 
                                        Click="artistButton_Click"
                                        Visibility="{Binding Converter={StaticResource anv}}"
                                        FontSize="{Binding Converter={StaticResource fontSiz}}" ></Button></DataTemplate></ListBox.ItemTemplate></ListBox></Grid></Grid></Border></Window>

As you can see both the properties (HorizontalContentAlignment and VerticalContentAlignment are not used in the XAML. 

Now if I change the ListBox to ItemsControl the errors go away.  The problem with this is that I loose the selected item now.  Why would Listbox give an error and not the ItemsControl?

TIA,

LS


Lloyd Sheen


Viewing all articles
Browse latest Browse all 18858

Trending Articles