[英]WPF ObservableCollection<T> get ui reference from the added element
I have encountered a problem, I would like to have an UI reference returned (after an item has been added to the ObservableCollection). 我遇到了一个问题,我想返回一个UI引用(将一个项目添加到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)? XAML,我要返回的是Datatemplate中的Border(可能)吗?
<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. 不幸的是, ObservableCollection
没有提供您想要的功能。 You may use ItemsControl.ItemContainerGenerator.ContainerFromItem()
to obtain ContentControl
that wraps your DataTemplate
. 您可以使用ItemsControl.ItemContainerGenerator.ContainerFromItem()
来获取包装DataTemplate
ContentControl
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.