2016-05-19 10 views
0

xamlは次のようなユーザーコントロールがあります。問題があるcsファイルのVisualChild行を見てください。私がUserControlを見つけようとすると、ブレークポイントを保持しています。それはnullなので、textblock要素を見つけることができません。私のVisualChildは、この場所で同じコードです。 How can I find WPF controls by name or type? 助けてください。wpf UserControl、FindChildは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> 

私の背後にあるコードは、あなたがこのDataTemplateのを使用している

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); 
      } 
     } 


    } 

答えて

0

を持っています?それはリストボックスにありますか? FrameworkTemplate.FindNameメソッドを使用してコントロールにアクセスできるはずです。 ContentPresenterからのDataTemplateを取得後

ContentPresenter contentPresenter = FindVisualChild<ContentPresenter>(yourListBoxItemName); 

:まず、ListBoxItemsのいずれかからのContentPresenterを取得

DataTemplate yourDataTemplate = contentPresenter.ContentTemplate; 

その後のDataTemplateからのMediaElementを得る:

TextBlock yourTextBox = yourDataTemplate.FindName("tblAlertId", contentPresenter) 
as TextBlock; 
if (yourTextBox != null) 
{ 
    alertId = Convert.ToInt32(item.Text); 
executeAlertClickCommand(ConsoleSettingsModel.GetInstance().SettingsCommandsData.AlertCommand, alertId); 
} 

ご覧ください。詳細については、MSDNのFrameworkTemplate.FindNameメソッドページを参照してください。 http://msdn.microsoft.com/en-us/library/system.windows.frameworktemplate.findname.aspx

+0

Vishakhに感謝しますが、リストボックスでデータテンプレートを使用していません。そこで私はDevExpress INotificationViewインターフェイスを使用しています。このインターフェイスでは、この通知ビューを別のクラスに存在するインターフェイスに渡して、警告が表示されます。アラートがクリックされたら、何かしたいです。それは私がBorder_Clickを持っている理由です。ですから、コンテンツプレゼンターを見つける方法を教えてください。私はcsファイルにテキストブロックテキストが必要です。 – nikhil

+0

私はコンテンツプレゼンターがここで通知ビューになると思う。データテンプレートを使用して通知ビューを表示しているためです。それを試してみてください。 – ViVi

0

は、(。!dpParent.GetType()BaseType = typeof演算(のTextBlock))しながら、ラインを交換

private TextBlock GetTopParent() 
    { 
     DependencyObject dpParent = this.Parent; 
     do 
     { 
      dpParent = LogicalTreeHelper.GetParent(dpParent); 
     } while (dpParent.GetType().BaseType != typeof(UserControl)); 
     return dpParent as TextBlock; 
    } 

、などのLogiclTreeHelperクラスを使用してテキストブロックを探してみてください。

+0

確かに試してみます。ありがとう。 – nikhil

+0

値にnullを設定できないなどの例外が発生します。パラメータ名:current この行にdpParent = LogicalTreeHelper.GetParent(dpParent); – nikhil

+0

こんにちは、Nikhil申し訳ありませんが、ユーザーコントロールやMainWindowなどのTextblockを見つけたい、またはGridであることができるその引数に現在の親要素を渡す必要があります。 –

関連する問題