Edgar
Edgar

Reputation: 64

Binding Error 4 on Listview-Style

I wrote the following styles

<Style x:Key="StyleListViewItems" TargetType="ListView">
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
</Style>


<DataTemplate x:Key="DataTemplateListViewItems">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Image Grid.Row="0" Width="175" Height="175" Source="{controls:ConcatString AttachString=/JPMorganFxxR;component/Content/Icons/Items/, BindTo={Binding Path=Image}}"/>
        <TextBlock Grid.Row="1" Height="24" Width="175" Text="{Binding Path=Name, Mode=OneWay}" IsEnabled="False" TextWrapping="WrapWithOverflow"
                       TextAlignment="Center" FontSize="16"/>
    </Grid>
</DataTemplate>

I use them in an listview:

<ListView Name="LstItems" Width="Auto" Height="500" Margin="25" SelectionMode="Single" Background="Cornsilk"
                  ItemTemplate="{StaticResource DataTemplateListViewItems}" 
                  ItemsSource="{Binding FxData.Item}" 
                  Style="{StaticResource StyleListViewItems}" 
                  ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        </ListView>

This works pretty well, but I get some errors I don't unterstand:

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')

Why does the program dry's to bind to Alignments at the moment I use a Wrappanel as an ItemsPanelTemplate (without thisit works without errors) or why DataItem is null when it is working with the given itemsource. In examples, which can be found everywhere in the internet, its done exactly the same way.

Thanks in advance.

Upvotes: 0

Views: 364

Answers (1)

punker76
punker76

Reputation: 14601

this can be fixed by setting the default content alignements

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

original tipp

Upvotes: 1

Related Questions