从外部UserControl

时间:2015-08-18 08:09:59

标签: c# wpf user-controls event-handling datatemplate

为了使WPF项目更加清晰,我决定在外部UserControl中定义ItemsControl行的布局。

这是显示列表的主页:

<ScrollViewer Name="ItemsScroll" Grid.Row="1" VerticalScrollBarVisibility="Auto">
    <ItemsControl Name="ItemsList">
        <ItemsControl.Resources>
            <DataTemplate DataType="{x:Type local:OptionSetItem}">
                <local.controls:OptionItemControl />
            </DataTemplate>
        </ItemsControl.Resources>
    </ItemsControl>
</ScrollViewer>

local.controls:OptionItemControl在另一个文件中定义:

<UserControl x:Class="OptionSetManager.Controls.OptionItemControl"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="100" d:DesignWidth="300">

    <Grid Height="30">
        <Grid.Resources>
            <BooleanToVisibilityConverter x:Key="BoolToVis" />
        </Grid.Resources>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="2*" />
            <ColumnDefinition Width="7*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>

        <TextBlock Name="ItemId" Grid.Column="0" VerticalAlignment="Center" TextAlignment="Center" FontSize="16" Foreground="DarkGray" Text="{Binding Id}" />
        <TextBox Name="ItemText" Grid.Column="1" VerticalContentAlignment="Center" FontSize="16" Text="{Binding Text}" BorderBrush="{x:Null}" Background="#FFF0F0F0" IsReadOnly="{Binding ToBeDeleted}" HorizontalAlignment="Right" Width="210" />
        <Button Name="DeleteExistingButton" Grid.Column="2" Click="DeleteExistingButton_Click" Tag="{Binding Id}" Cursor="Hand" ToolTip="Mark as deleted">
            <Button.Template>
                <ControlTemplate>
                    <Image Source="/Assets/delete.png"/>
                </ControlTemplate>
            </Button.Template>
        </Button>

        <Rectangle Fill="Red" Height="3" Margin="15,0" Grid.Column="0" Visibility="{Binding ToBeDeleted, Converter={StaticResource BoolToVis}}"></Rectangle>

    </Grid>
</UserControl>

如您所见,自定义控件包含一个用于触发Click事件的DeleteExistingButton。目标是从列表所在的主页面中捕获此事件。

我跟着像this这样的答案,但我已经陷入了某个时刻我会告诉你的。首先,这是UserControl背后的代码:

namespace OptionSetManager.Controls
{
    public partial class OptionItemControl : UserControl
    {
        public event EventHandler OnDeleteExistingButtonClick;

        public OptionItemControl()
        {
            InitializeComponent();
        }

        private void DeleteExistingButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.OnDeleteExistingButtonClick != null) OnDeleteExistingButtonClick(this, e);
        }
    }
}

这里我将事件传播给处理程序(如果存在)。我应该从主页面附上它,但显然我不能做像

这样的事情
OptionItemControl.OnDeleteExistingButtonClick += new EventHandler(UserControl_ButtonClick);

因为OptionItemControl不是对象的实例,而只是DataTemplate中的一个元素。

我错过了什么?

1 个答案:

答案 0 :(得分:0)

您可以使用以下语法处理DataTemplate中的OnDeleteExistingButtonClick事件:

<DataTemplate DataType="{x:Type local:OptionSetItem}">
    <local.controls:OptionItemControl OnDeleteExistingButtonClick="YourHandler"/>
</DataTemplate>

注意:您应该将事件命名为DeleteExistingButtonClick而不是OnDeleteExistingButtonClick(这是一个约定,引发事件的函数应命名为OnYourEvent或RaiseYourEvent)