2012-08-07 87 views
0

我有三個用戶控件。從UserControl的Parent調用UserControl的listview按鈕點擊事件

用戶控制1

<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:local="clr-namespace:Gramercy" 
    mc:Ignorable="d" 
    x:Class="wpfapplication.UCManageQALibrary" 
    x:Name="UserControl" 
    d:DesignWidth="774" d:DesignHeight="529.723"> 

    <Grid x:Name="LayoutRoot"> 
     <Grid> 
      <Button Content="List QA" x:Name="btnSearchQAStructure" Click="Button_Click_1" /> 
      <Button Content="Add New QA" x:Name="btnAddNewQAStructure" Click="Button_Click" /> 

      <local:UCSearchQALibrary x:Name="searchQALibrary" Visibility="Visible"/> 
        <local:AddNewQA x:Name="addQALibrary" Visibility="Hidden"/>   


     </Grid> 
    </Grid> 
</UserControl> 

用戶控制2

<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006 
    mc:Ignorable="d" 
    x:Class="wpfapplication.AddNewQA" 
    x:Name="UserControl" 
    d:DesignWidth="1280" d:DesignHeight="1024"> 

    <Grid x:Name="LayoutRoot"> 
     <Grid> 
     <Label x:name="Label1" Content="" /> 

     </Grid> 
    </Grid> 
</UserControl> 

用戶控制3

<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    mc:Ignorable="d" 
    x:Class="wpfapplication.UCSearchQALibrary" 
    x:Name="UserControl" 
    d:DesignWidth="758" d:DesignHeight="486.905"> 

    <Grid x:Name="LayoutRoot" > 
     <GroupBox Header="Question and Answer Master Library" >      
     <Grid Margin="8"> 
      <WrapPanel Margin="4,0,0,0" VerticalAlignment="Stretch"> 

      <ListView Margin="4,10,0,10" x:Name="lvQA" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ScrollViewer.VerticalScrollBarVisibility="Auto" AlternationCount="2" VerticalAlignment="Stretch" Height="420"> 
          <ListView.View> 
           <GridView> 
            <GridViewColumn > 
             <GridViewColumn.HeaderTemplate> 
              <DataTemplate> 
               <Label Content="Q &amp; A Search Result : " FontSize="12" FontWeight="Bold" Margin="0,4,0,4"/> 
              </DataTemplate>            
             </GridViewColumn.HeaderTemplate> 
             <GridViewColumn.CellTemplate> 
              <DataTemplate> 
               <StackPanel> 
                <Button Content="{Binding Path=QuestionText}" Margin="4,10,8,4" Style="{StaticResource HyperlinkLikeButton}" VerticalAlignment="Top" Click="Button_Click" CommandParameter="{Binding Path=QuestionID}" /> 
                <TextBlock TextWrapping="Wrap" Margin="4,0,4,4" HorizontalAlignment="Stretch"><Run Text="{Binding Path=AnswerText}"/></TextBlock> 
               </StackPanel> 
              </DataTemplate> 
             </GridViewColumn.CellTemplate> 
            </GridViewColumn> 
           </GridView> 
          </ListView.View>        
         </ListView> 
        </WrapPanel> 
     </Grid> 
     </GroupBox> 
    </Grid> 
</UserControl> 

使用r控制2用於添加新的問題答案,用戶控制3提供已創建的問題答案的列表。用戶控件1同時具有用戶控件2和3.每次用戶控件2或3都可見。在用戶控件1中,單擊List QA按鈕時,用戶控件3可見並且用戶控件2不可見。點擊添加新QA按鈕,用戶控件2可見並且用戶控件3不可見。

現在,在用戶控件3中,我有一個按鈕與listview的每一行關聯。在點擊/單擊該按鈕時,我需要一個功能來使用戶控件2可見,並將用戶控件2中的標籤與特定內容綁定。

我曾嘗試路由事件。

在用戶控制3 I加入了...

 public static readonly RoutedEvent AddClickEvent = EventManager.RegisterRoutedEvent("AddClick", RoutingStrategy.Direct, typeof(RoutedEventHandler), typeof(Button)); 

     public event RoutedEventHandler AddClick 
     { 
      add { AddHandler(AddClickEvent, value); } 
      remove { RemoveHandler(AddClickEvent, value); } 
     } 

     void RaiseAddClickEvent() 
     { 
      RoutedEventArgs newEventArgs = new RoutedEventArgs(UCSearchQALibrary.AddClickEvent); 
     } 
     private void Button_Click(object sender, System.Windows.RoutedEventArgs e) 
     { 
      // TODO: Add event handler implementation here. 
      //Thread newWindowThread = new Thread(new ThreadStart(ThreadStartingPoint)); 
      //newWindowThread.SetApartmentState(ApartmentState.STA); 
      //newWindowThread.IsBackground = true; 
      //newWindowThread.Start();  
      RaiseEvent(new RoutedEventArgs(AddClickEvent)); 
     } 

,並在用戶控制1 I加入...

local:UCSearchQALibrary.AddClick="dCB_Props_AddClick" 

private void dCB_Props_AddClick(object sender, System.Windows.RoutedEventArgs e) 
{ 
    MessageBox.Show("This Works"); 
}   

任何幫助將高度讚賞。

謝謝。

回答

1

您可以創建一個事件路由到usercontrol1。 但更好的方法是創建一個視圖模型(您也可以爲所有3個用戶控件使用一個視圖模型)。

因此,您在Viewmodel中創建一個2屬性,並鍵入Visibility類型並將usercontrol1和2綁定到它們。 (綁定在usercontrol1中)。

+0

我曾嘗試路由事件,但沒有成功。 – 2012-08-07 11:24:42

+0

我不想使用視圖模型,我不是專家。 – 2012-08-07 11:25:13

+0

請參閱編輯的代碼。 – 2012-08-07 11:30:49

相關問題