从UserControl的Parent调用UserControl的listview的按钮单击事件

时间:2012-08-07 10:01:57

标签: wpf listview user-controls wpf-controls

我有三个用户控件。

用户控制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>

用户控件2用于添加新的问题答案,用户控件3提供已创建的问题答案列表。用户控件1具有用户控件2和3.用户控件2或3都可见。在用户控件1中,单击列表QA按钮时,用户控件3可见,用户控件2不可见。单击“添加新QA”按钮时,用户控件2可见,用户控件3不可见。

现在,在用户控件3中,我有一个与listview的每一行相关联的按钮。在点击/单击该按钮时,我需要一个功能来使用户控件2可见,并将用户控件2内的标签与特定内容绑定。

我曾尝试过路由活动。

在用户控件3中我添加了...

        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中我添加了...

local:UCSearchQALibrary.AddClick="dCB_Props_AddClick"

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

任何帮助都将受到高度赞赏。

感谢。

1 个答案:

答案 0 :(得分:1)

您可以创建一个路由到usercontrol1的事件。 但更好的方法是创建一个viewmodel(你也可以使用一个用于所有3个用户控件)。

因此,您在Viewmodel中创建一个类型为Visibility类型的属性,并将usercontrol1和2绑定到它们。 (绑定在usercontrol1中)。