如何在使用UserControls时实现按钮处理程序

本文关键字:实现 按钮 处理 程序 UserControls | 更新日期: 2023-09-27 18:02:51

我正在设计一个应用程序,我将有一个模板化的UI格式,将枚举TabItems基于它从数据库返回的对象的数量(<8).为了动态地生成它们,我创建了一个包含选项卡UI内所有对象的动态资源,并将资源分配给每个TabItem的content属性。工作很棒,但是现在我无法弄清楚如何修改UI(和实现处理程序)与这个设置。

我在窗口的xaml中添加了一个临时的Test TabItem,分配了资源,但是我不能修改窗口内的任何项目。

我应该使用UserControls以外的东西吗?这些选项卡将具有完全相同的功能,只是需要通过选项卡浏览不同的数据集。数据类型将完全相同。

编辑3:

<Window
    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:<Redacted>.WPF"
    xmlns:ToggleSwitch="clr-namespace:ToggleSwitch;assembly=ToggleSwitch" x:Class="<Redacted>.WPF.MainWindow"
    mc:Ignorable="d"
    Title="<Redacted>" Height="704" Width="1203.3">
<Window.Resources>
    <UserControl x:Key="RemoteSiteUserControl">
        <Grid Background="#FFE5E5E5">
            <Grid.RowDefinitions>
                <RowDefinition Height="334*"/>
                <RowDefinition Height="257*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="10*"/>
                <ColumnDefinition Width="31*"/>
            </Grid.ColumnDefinitions>
            <ListView x:Name="listViewMachines" HorizontalAlignment="Left" Height="252" Margin="10,34,0,0" VerticalAlignment="Top" Width="261">
                <ListView.View>
                    <GridView>
                        <GridViewColumn/>
                    </GridView>
                </ListView.View>
            </ListView>
            <Label x:Name="lblMachines" Content="Machines:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="26" Width="122"/>
            <Label x:Name="lblNotifications" Content="Notification Queue:" Grid.Column="1" HorizontalAlignment="Left" Height="32" Margin="10,55,0,0" Grid.Row="1" VerticalAlignment="Top" Width="167"/>
            <ListView x:Name="listViewNotifications" Grid.Column="1" HorizontalAlignment="Left" Height="160" Margin="23,87,0,0" Grid.Row="1" VerticalAlignment="Top" Width="855">
                <ListView.View>
                    <GridView>
                        <GridViewColumn/>
                    </GridView>
                </ListView.View>
            </ListView>
            <Button x:Name="btnAddMachine" Content="Add" HorizontalAlignment="Left" Margin="10,300,0,0" VerticalAlignment="Top" Width="75" Click="btnAddMachine_Click"/>
            <Button x:Name="btnModifyMachine" Content="Modify" HorizontalAlignment="Left" Margin="104,300,0,0" VerticalAlignment="Top" Width="75" Click="btnModifyMachine_Click"/>
            <Button x:Name="btnDelete" Content="Delete" HorizontalAlignment="Left" Margin="196,300,0,0" VerticalAlignment="Top" Width="75" Click="btnDelete_Click"/>
            <Label x:Name="lblIpAddress" Content="Ip Address:" HorizontalAlignment="Left" Height="23" Margin="49,81,0,0" VerticalAlignment="Top" Width="62" FontSize="10.667" RenderTransformOrigin="0.784,0.435" Grid.Row="1"/>
            <Label x:Name="lblMachineName" Content="Machine Name:" HorizontalAlignment="Left" Height="23" Margin="28,25,0,0" VerticalAlignment="Top" Width="83" FontSize="10.667" RenderTransformOrigin="0.602,0.565" Grid.Row="1"/>
            <Label x:Name="lblAlias" Content="Alias:" HorizontalAlignment="Left" Height="23" Margin="77,53,0,0" VerticalAlignment="Top" Width="34" FontSize="10.667" Grid.Row="1"/>
            <TextBox x:Name="txtMachineName" HorizontalAlignment="Left" Height="23" Margin="116,25,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="136" FontSize="10.667" FontFamily="Arial" Grid.Row="1" IsReadOnly="True" IsReadOnlyCaretVisible="True"/>
            <TextBox x:Name="txtIpAddress" HorizontalAlignment="Left" Height="23" Margin="116,81,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="136" Grid.Row="1" IsReadOnly="True" IsReadOnlyCaretVisible="True"/>
            <TextBox x:Name="txtAlias" HorizontalAlignment="Left" Height="23" Margin="116,53,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="136" FontSize="10.667" FontFamily="Arial" Grid.Row="1" IsReadOnly="True" IsReadOnlyCaretVisible="True"/>
            <Label x:Name="lblStatus" Content="Status:" Grid.Column="1" HorizontalAlignment="Left" Margin="23,32,0,0" VerticalAlignment="Top" Width="53" FontSize="14.667"/>
            <Ellipse Name="ellipseStatus" Grid.Column="1" Fill="Red" HorizontalAlignment="Left" Height="18" Margin="100,37,0,0" Stroke="Black" VerticalAlignment="Top" Width="18"/>
            <Label x:Name="lblPerfCounters" Content="Performance Counters:" Grid.Column="1" HorizontalAlignment="Left" Height="30" Margin="532,23,0,0" VerticalAlignment="Top" Width="161" FontSize="14.667"/>
            <ToggleSwitch:HorizontalToggleSwitch Name="togglePerfCounters" Grid.Column="1" HorizontalAlignment="Left" Margin="747,31,0,0" VerticalAlignment="Top" Height="19" Width="76"/>
            <Label x:Name="lvlServices" Content="Services:" Grid.Column="1" HorizontalAlignment="Left" Height="30" Margin="23,84,0,0" VerticalAlignment="Top" Width="74" FontSize="14.667"/>
            <ListView x:Name="listViewServices" Grid.Column="1" HorizontalAlignment="Left" Height="229" Margin="23,114,0,0" VerticalAlignment="Top" Width="409" Grid.RowSpan="2">
                <ListView.View>
                    <GridView>
                        <GridViewColumn/>
                    </GridView>
                </ListView.View>
            </ListView>
            <Button x:Name="btnDeleteServiceMonitor" Content="Delete" HorizontalAlignment="Left" Margin="357,22,0,0" VerticalAlignment="Top" Width="75" Grid.Column="1" Grid.Row="1" Click="btnDeleteServiceMonitor_Click"/>
            <Button x:Name="btnAddServiceMonitor" Content="Add" HorizontalAlignment="Left" Margin="266,22,0,0" VerticalAlignment="Top" Width="75" Grid.Column="1" Grid.Row="1" Click="btnAddServiceMonitor_Click"/>
            <TextBox x:Name="txtCpuTimer" HorizontalAlignment="Left" Height="30" Margin="687,73,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="136" FontSize="10.667" FontFamily="Arial" IsReadOnly="True" IsReadOnlyCaretVisible="True" Grid.Column="1"/>
            <Label x:Name="lblCpuTimer" Content="Cpu Timer(sec):" Grid.Column="1" HorizontalAlignment="Left" Height="30" Margin="588,73,0,0" VerticalAlignment="Top" Width="94"/>
            <Label x:Name="lblCpuUsage" Content="Cpu Usage(%):" Grid.Column="1" HorizontalAlignment="Left" Height="30" Margin="593,114,0,0" VerticalAlignment="Top" Width="94" RenderTransformOrigin="0.734,0.4"/>
            <TextBox x:Name="txtCpuUsage" HorizontalAlignment="Left" Height="30" Margin="687,114,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="136" FontSize="10.667" FontFamily="Arial" IsReadOnly="True" IsReadOnlyCaretVisible="True" Grid.Column="1"/>
            <Label x:Name="lblCpuPerformance" Content="CPU Performance:" Grid.Column="1" HorizontalAlignment="Left" Height="33" Margin="543,200,0,0" VerticalAlignment="Top" Width="144" FontSize="14.667"/>
            <Label x:Name="labelDescription" Content="Additional Information:" HorizontalAlignment="Left" Margin="10,109,0,0" Grid.Row="1" VerticalAlignment="Top"/>
            <TextBox x:Name="txtDescription" HorizontalAlignment="Left" Height="107" Margin="10,140,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="261"/>
        </Grid>
    </UserControl>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="27*"/>
        <RowDefinition Height="619*"/>
        <RowDefinition Height="27*"/>
    </Grid.RowDefinitions>
    <Menu x:Name="menu" HorizontalAlignment="Stretch" Margin="0,0,0,0" Grid.RowSpan="1" VerticalAlignment="Stretch">
        <MenuItem Header="_File" x:Name="menuFile" HorizontalAlignment="Center" Height="27" Width="28">
        </MenuItem>
        <MenuItem Header="_Edit" x:Name="menuEdit" HorizontalAlignment="Center"/>
        <MenuItem Header="_Tools" x:Name="menuTools" HorizontalAlignment="Center">
            <MenuItem Header="_Manage Remote Sites" x:Name="menuManageRemoteSItes" HorizontalAlignment="Left"/>
        </MenuItem>
        <MenuItem Header="_Windows" x:Name="menuWindows" HorizontalAlignment="Center"/>
        <MenuItem Header="_Reports" x:Name="menuReports" HorizontalAlignment="Center"/>
        <MenuItem Header="_Help" x:Name="menuHelp" HorizontalAlignment="Center"/>
    </Menu>
    <TabControl x:Name="tabControl" HorizontalAlignment="Stretch" Margin="0,0,0,0" Grid.Row="1" VerticalAlignment="Stretch" SelectionChanged="tabControl_SelectionChanged">
        <TabItem Header="Test" Content="{DynamicResource RemoteSiteUserControl}"/>
    </TabControl>
</Grid>

如何在使用UserControls时实现按钮处理程序

为了解决您设计中的直接问题,我会避免Click事件,而是使用带有DataContext的Commands。您可以将TabItem的数据上下文分配给单独的c#类(视图模型),然后绑定到资源中的命令。

<TabItem DataContext="{Binding MyViewModelDataContext}" />

注意MyViewModelDataContext必须在你的.xaml.cs代码中作为一个公共属性实例化,这样它才能被绑定。

你可以在模板中使用命令,因为它是一个绑定:

<Button Command="{Binding MyCommand}" />

,其中命令在MyViewModelDataContext中实现。

下面是一个有用的命令概述:如何在WPF中绑定命令

我也使用DevExpress的MVVM框架,他们有一个非常好的命令实现。

还有:您的整个XAML代码中没有绑定。阅读绑定;它会帮助你的。这里有一个介绍绑定的链接:http://blog.scottlogic.com/2012/04/20/everything-you-wanted-to-know-about-databinding-in-wpf-silverlight-and-wp7-part-two.html

希望对你有帮助。