Ich habe eine Benutzersteuerung, wo der Xaml wie folgt ist. Bitte beachten Sie die VisualChild-Zeile in der CS-Datei, die das Problem ist. Wenn ich versuche, das Benutzersteuerelement zu finden, behalte ich einen Unterbrechungspunkt, es ist null und folglich kann ich das Textblockelement nicht finden. Mein VisualChild ist der gleiche Code an diesem Ort. How can I find WPF controls by name or type? Bitte helfen Sie.wpf UserControl, FindChild gibt null
XAML:
<UserControl Name="NFView" x:Class="AthenaIsolatedFeatures.ProximityAlerts.Views.NotificationView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:AthenaIsolatedFeatures.ProximityAlerts.Views"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<DataTemplate x:Key="CustomNotificationTemplate">
<Border Name="border" BorderBrush="Black" BorderThickness="1" MouseLeftButtonDown="Border_MouseLeftButtonDown">
<!--<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDown">
<i:InvokeCommandAction Command="{Binding AlertClickCommand}" CommandParameter="{Binding}"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>-->
<DockPanel LastChildFill="True">
<StackPanel DockPanel.Dock="Top" Grid.Row="0" Background="Red">
<TextBlock Text="Proximity Alert" HorizontalAlignment="Left"></TextBlock>
</StackPanel>
<Grid Name="GRD2" Background="#FFB6C1" DockPanel.Dock="Bottom" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<!--<RowDefinition Height="Auto"></RowDefinition>-->
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Image Source="{Binding Source}" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" Margin="4,-25,0,0" Height="100" Width="65"></Image>
<TextBlock Grid.Row="0" Margin="25,5,0,0" Grid.Column="1" Text="{Binding AlertDescription}" FontSize="15" FontWeight="Bold"></TextBlock>
<!--<TextBlock Grid.Row="1" Grid.Column="1" Margin="7,1,0,0" Text="{Binding requestId}"></TextBlock>-->
<TextBlock Grid.Row="1" Margin="25,-28,0,0" Grid.Column="1" Text="{Binding requestId,StringFormat='Session: {0}'}"></TextBlock>
<TextBlock Grid.Row="0" Grid.Column="2" HorizontalAlignment="Right" Margin="39,5,0,0" Text="{Binding alertTimeStamp}"></TextBlock>
<TextBlock Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" Margin="25,-30,0,0" Text="{Binding AlertText}"></TextBlock>
<TextBlock Name="tblAlertId" Grid.Row="2" Grid.Column="1" Text="{Binding alertId}" Visibility="Collapsed"></TextBlock>
</Grid>
</DockPanel>
</Border>
</DataTemplate>
</UserControl.Resources>
<Grid>
</Grid>
</UserControl>
Der Code hinter ich
public partial class NotificationView : UserControl
{
public int alertId { get; set; }
public NotificationView()
{
InitializeComponent();
}
private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var item = FindVisualChild.FindChild<TextBlock>(NFView, "tblAlertId"); //Problem is this line.
alertId = Convert.ToInt32(item.Text);
executeAlertClickCommand(ConsoleSettingsModel.GetInstance().SettingsCommandsData.AlertCommand, alertId);
}
internal void executeAlertClickCommand(WSMgrCommands cmd, int id) //Raising custom command
{
var wsParams = new WSAcknoledgedAlert();
wsParams.alertId = id;
if (cmd.CanExecute(wsParams))
{
cmd.Execute(wsParams);
}
}
}
Danke Vishakh, aber ich verwende nicht die Datenvorlage in einer Listbox. Daher verwende ich eine DevExpress INotificationView-Schnittstelle, in der ich diese Benachrichtigungsansicht an die Schnittstelle übergebe, die in einer anderen Klasse vorhanden ist, sodass Warnungen angezeigt werden. Wenn eine Warnung angeklickt wird, möchte ich etwas tun. Deshalb habe ich Border_Click. So können Sie mir helfen, wie man den Inhaltsvorführer findet. Ich benötige nur den Textblocktext in der cs Akte. – nikhil
Ich denke, der Content-Presenter kann hier die Benachrichtigungsansicht sein. Da Sie die Datenvorlage verwenden, um die Benachrichtigungsansicht anzuzeigen. Versuche es damit. – ViVi