I have an ItemsControl. For the last item in the ItemsControl I want to hide the TextBox containing the comma. Is there a way to do this using XAML?
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Visibility="{Binding Value, Converter={StaticResource NotEmpty}}">
<TextBlock Text="{Binding QuestionNam开发者_运维百科e}" />
<TextBlock Text=" " />
<TextBlock Text="{Binding Answer}"/>
<TextBlock Text=", " />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
How about
<TextBlock Text=", " Visibility="{Binding LastItemVisibility}" />
with in your view model something like
public Visibility LastItemVisibility
{
get { return MyCollection.LastOrDefault() == this ? Visibility.Collapsed : Visibility.Visible; }
}
?
It's annoying it's not easier to solve this with a Converter. In fact if you could bind to the ConverterParameter (which is not possible in Silverlight v4) you could achieve what you want quite easily.
If you don't want to touch your model I think your best bet would be to create a new class derived from ControlControl which sets its own visibility based on the position of a bounditem in the itemssource. It's not the neatest solution in the world, but it keeps the model clean. It would look like this in the ItemsControl
<local:ItemsControlVisibilityHelper ShowIfLast="False" ShowIfFirst="True" ShowIfNotLastOrFirst="True"
ItemsControl="{Binding ElementName=x_ItemsControl}"
BoundItem="{Binding}"
>
<TextBlock Text=", "></TextBlock>
</local:ItemsControlVisibilityHelper>
精彩评论