DermFrench
DermFrench

Reputation: 4057

Binding to item instead of property inside list box

I have a WPF list box, I'm currently using a visibility converter bound to Returns on the current item. However instead of passing in the value of returns I want to pass in the object.

I've tried using / instead of Path=Returns but it no longer calls the converter. Any ideas on what I'm doing wrong?

<ListBox Name="BetsListBox" BorderThickness="0"  ItemsSource="{Binding Path=Wagers}"  FontFamily="Arial Black" FontSize="12">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel  Orientation="Horizontal">
                <TextBlock FontFamily="Arial" Text="{Binding Path=UnitStake, StringFormat={}{0:N2}}" />
                <TextBlock FontFamily="Arial" Text="{Binding Path=WagerType}" Margin="5,0" />
                <TextBlock FontFamily="Arial" Text="{Binding Path=CalculatedStake, StringFormat={}{0:N2}}"  />
                <StackPanel Margin="-5,0" Visibility="{Binding Path=Returns,Converter={StaticResource BetWagerPotentialReturnToVisibilityConverter1}}" Orientation="Horizontal">
                    <TextBlock HorizontalAlignment="Left" FontWeight="Bold" Text=", Bet Returns"  FontFamily="Arial"  FontStretch="ExtraCondensed" Margin="5,0,5,0"/>
                    <TextBlock FontWeight="Bold" Text="{Binding PotentialReturns, StringFormat={}{0:N2}}" ToolTip="{Binding Name}" FontFamily="Arial" FontStretch="ExtraCondensed" />
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Upvotes: 1

Views: 356

Answers (3)

har07
har07

Reputation: 89295

In binding, when you don't specify the path it means bind to current DataContext which in this case is respective item from ListBox's ItemsSource :

<StackPanel Visibility="{Binding Converter={StaticResource 
    BetWagerPotentialReturnToVisibilityConverter1}}" >

Upvotes: 1

Sheridan
Sheridan

Reputation: 69979

You can just "{Binding}" if there is nothing else in the markup for that Binding, but if you want to add a Converter, then you'll have to use the . notation to say 'this object':

<StackPanel Margin="-5,0" Visibility="{Binding Path=., Converter={StaticResource 
    BetWagerPotentialReturnToVisibilityConverter1}}" Orientation="Horizontal">
    ...
</StackPanel>

You can find out more from the Binding.Path Property page on MSDN.


UPDATE >>>

To clarify the situation, there are a number of ways that you can data bind to a whole object in XAML:

If you have a comma here, then you will need to use the . notation to avoid a compilation error:

<StackPanel Margin="-5,0" Visibility="{Binding ., Converter={StaticResource 
    BetWagerPotentialReturnToVisibilityConverter1}}" Orientation="Horizontal">
    ...
</StackPanel>

If you have no comma here, then you will not need to use the . notation:

<StackPanel Margin="-5,0" Visibility="{Binding Converter={StaticResource 
    BetWagerPotentialReturnToVisibilityConverter1}}" Orientation="Horizontal">
    ...
</StackPanel>

Also, setting the Path (as in the original example) is optional and this keyword can be omitted (as in the two examples above).

Upvotes: 1

Stuart Grassie
Stuart Grassie

Reputation: 3073

Have you tried using the Binding directly?

Edit:

My bad, forgot to remove the comma:

Visibility="{Binding Converter={StaticResource BetWagerPotentialReturnToVisibilityConverter1}}"

Upvotes: 5

Related Questions