Wpf 调用UserControl';s listview';用户控件中的s按钮单击事件';父母

Wpf 调用UserControl';s listview';用户控件中的s按钮单击事件';父母,wpf,listview,user-controls,wpf-controls,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://sche

我有三个用户控件

用户控件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>
在用户控制1中,我添加了

local:UCSearchQALibrary.AddClick="dCB_Props_AddClick"

private void dCB_Props_AddClick(object sender, System.Windows.RoutedEventArgs e)
{
     MessageBox.Show("This Works");
}        
我们将非常感谢您的帮助


谢谢

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


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

我不想使用视图模型,我不是这方面的专家。因此,您可以在视图模型中为当前触发单击事件的每个按钮创建一个命令。将相同的Viewmodel实例应用于所有3个Usercontrols(只需在Usercontrol1的ctor中创建一个新实例并将其传递给2和3)。将Viewmodel设置为datacontext。现在,在xaml中创建与viewmodel中定义的命令和属性的绑定。例如,按钮的命令和usercontrol1和2的VisibilityProperty。你不会在后台有任何代码
<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>
        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));
        }
local:UCSearchQALibrary.AddClick="dCB_Props_AddClick"

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