I have encountered a problem, I would like to have an UI reference returned (after an item has been added to the ObservableCollection). So when an item has been added to the collection, I would like to get the "Border" reference.
Been trying to google for a solution but I cant find one.
The XAML, what I want is to return the Border in Datatemplate (is this possible)?
<ItemsControl ItemsSource="{Binding Elements}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Margin="37,0,0,0" Orientation="Horizontal" HorizontalAlignment="Left" Width="590" Height="29" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemContainerStyle>
<Style TargetType="{x:Type ContentPresenter}" />
</ItemsControl.ItemContainerStyle>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Width="{Binding Style.Background.Width}" Margin="{Binding Style.Background.Position, Converter={StaticResource LeftMarginConverter}}" BorderThickness="{Binding Style.Background.BorderSize}" BorderBrush="{Binding Style.Background.BorderColor, Converter= {StaticResource MediaBrushConverter}}" Height="27" HorizontalAlignment="Center" VerticalAlignment="Center">
<Border.Background>
<LinearGradientBrush GradientStops="{Binding Style.Background.Background, Converter={StaticResource GradientConverter}}" Opacity="{Binding Style.Background.Opacity}"/>
</Border.Background>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock Text="{Binding Value}" FontFamily="{Binding Style.Value.Font, Converter= {StaticResource FontConverter}}" Foreground="{Binding Style.Value.FontColor, Converter= {StaticResource MediaBrushConverter}}" FontSize="{Binding Style.Value.FontSize}" TextAlignment="Center" Opacity="1" HorizontalAlignment="Center" VerticalAlignment="Center" />
<TextBlock Text="{Binding TextExtension}" FontFamily="{Binding Style.Value.Font, Converter= {StaticResource FontConverter}}" Foreground="#eaeaea" FontSize="{Binding Style.Value.FontSize}" TextAlignment="Center" Opacity="1" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
foreach(var x in Elements) // Elemets = ObservableCollection
{
var i =FControls.ItemContainerGenerator.ContainerFromItem(x) as Border;
MessageBox.Show(i.ToString()); /// Returns null
}
Unfortunately, ObservableCollection
doesn't provide the functionality you want. You may use ItemsControl.ItemContainerGenerator.ContainerFromItem()
to obtain ContentControl
that wraps your DataTemplate
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.