So I have this list of products, and the list is binded to the Products
property successfully, and all of the data for items (Name
, Description
, StartingPrice
) is binded successfully.
<ListBox ItemsSource="{Binding Products}"
HorizontalContentAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="110"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="60"/>
</Grid.ColumnDefinitions>
<Grid Grid.Column="0">
<Rectangle Width="100" Height="100" Fill="Black" Margin="0,0,0,0" />
</Grid>
<StackPanel Grid.Column="1">
<Label Content="{Binding Name}" Padding="0"/>
<Label Content="{Binding StartingPrice}" Padding="0"/>
<Label Content="Last Bidder" Padding="0"/>
<Label Content="Time left" Padding="0"/>
</StackPanel>
<Label Grid.Column="2" Content="{Binding Description}" Padding="0" />
<Grid Grid.Column="3">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Grid.Row="0" Content="Delete" Background="Red" Visibility="{Binding Path=DataContext.DeleteButtonVisibility}" />
<Button Grid.Row="1" Content="Bid" Background="LightGreen" Visibility="{Binding Path=DataContext.BidButtonVisibility}"/>
</Grid>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Now I want to show/hide Bid
and Delete
buttons.
This information is stored in DeleteButtonVisibility
and BidButtonVisibility
in the viewmodel where Products
is also stored, and they change as planned from what debugged.
How I can bind these properties from items in list that has it's own DataContext
?
CodePudding user response:
To bind the Visibility
properties of the Buttons with the properties of view model where Products
exists, you need to insert RelativeSource
to their bindings and make them to look up to the parent ListBox
.
Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBox}},
Path=DataContext.DeleteButtonVisibility, Mode=OneWay}