繁体   English   中英

如何从DataTemplate引发自定义的路由事件?

[英]How can I raise a custom Routed Event from a DataTemplate?

我有一个名为dCB_Props的用户控件,其中包含几个对象,最重要的是一个绑定到Observable集合的ComboBox。 尽管该集合可以使用任何对象,但通常将使用一个称为EditDeleteItem的UserControl。 我已将dCB_Props设置为使用EditDeleteItem作为ItemsTemplate但未触发事件。 另一方面,如果我添加EditDeleteItem的实例,则事件将被触发。 我无法以这种方式添加项目,因为EditDeleteItem将托管其他控件,并且我需要使用其他DataTemplates。

EditDeleteItem具有两个路由事件,称为EditClickDeleteClick

当集合更改时,它会触发一个事件,该事件检查添加的项是否为EditDeleteItem类型。 如果是这样,则将处理程序添加到上述两个事件中。

xaml for EditDeleteClick的一部分:

<WrapPanel x:Name="wp" HorizontalAlignment="Right" Visibility="Hidden" VerticalAlignment="Center" Margin="0,0,5,0">
    <Button x:Name="PART_Edit" Width="20" Height="20" Content="{DynamicResource dPen}" Style="{DynamicResource dTranspButton}" Click="btnEdit_Click"/> 
    <Button x:Name="PART_Delete" Width="20" Height="20" Content="{DynamicResource dCross}" Style="{DynamicResource dTranspButton}" Click="btnDelete_Click"/> 
</WrapPanel>
<Label Content="{TemplateBinding Content}"  Margin="2,0,45,0" Padding="0,0,0,0" HorizontalAlignment="Left" VerticalContentAlignment="Center"/>

xaml的dCB_Props的一部分:

<ComboBox HorizontalContentAlignment="Stretch" x:Name="PART_cb" Background="Transparent" Margin="0,0,0.367,0" d:LayoutOverrides="HorizontalAlignment" ItemsSource="{Binding Items, ElementName=dcb}" IsDropDownOpen="{Binding IsDropDownOpen,ElementName=dcb, Mode=TwoWay}" Grid.ColumnSpan="3" Style="{DynamicResource DaisyComboBox}" />
<Button x:Name="PART_Edit" Width="20" Height="20" Content="{DynamicResource dPen}" Visibility="Hidden" Style="{DynamicResource dTranspButton}" Margin="2.581,1.48,17.778,-1.48" Grid.Column="1" Click="btnEdit_Click"/>
<Button x:Name="PART_Delete" Width="20" Height="20" Content="{DynamicResource dCross}" Visibility="Hidden" Margin="22.602,1.48,-2.243,-1.48" Style="{DynamicResource dTranspButton}" Grid.Column="1" Click="btnDelete_Click"/>
<Button x:Name="PART_Add" Content="+" Grid.Column="3" Margin="0,0,0,0" Style="{DynamicResource dTranspButton}" Click="btnAdd_Click"/>

请注意,以上两个代码仅用于对象,我省略了“列定义”,“事件触发器”等。

dCB_Props.xaml.cs代码的一部分是:

public partial class dCB_Props : UserControl
{
    public dCB_Props()
    {
        this.InitializeComponent();
        Items= new ObservableCollection<object>();
        Items.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Items_CollectionChanged);
    }

    void Items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        if (e.Action == System.Collections.Specialized.NotifyCollectionChangedAction.Add)
        {
            foreach (var o in e.NewItems)
            {
                if (o.GetType() == typeof(EditDeleteItem))
                {
                    EditDeleteItem itm = (EditDeleteItem)o;
                    itm.EditClick += new RoutedEventHandler(ItemEdit_Click);
                    itm.DeleteClick += new RoutedEventHandler(ItemDelete_Click);
                }
            }
        }
    }
  ...//I've left some code here since I don't deem it's that important for the situation
    private void ItemEdit_Click(object sender, RoutedEventArgs e)
    {
        DependencyObject d = GetTemplateChild("PART_cb");
        if (d == null) return;
        ComboBox cb = (ComboBox)d;
        if (cb.SelectedItem != null) RaiseEvent(new RoutedEventArgs(EditClickEvent, e.OriginalSource));
    }
}

如果添加类型为EditDeleteItem的项目并删除EditDeleteItem内的Label的ItemTemplate属性,则上述方法dCB_Props 如果我在EditDeleteItem的ContentTemplate中设置如下所示的ItemTemplate,它也可以工作。 但是,如上所述,我需要使用不同的数据模板,因此我假设所有数据模板都必须驻留在资源字典中,然后必须使用模板选择器。

资料范本:

<DataTemplate x:Shared="false" x:Key="TagTemplate">
        <local:EditDeleteItem x:Name="edItem">
            <local:EditDeleteItem.Content>
                <StackPanel>
                    <TextBlock Text="{Binding Path=Content.Label}"/>
                    <CheckBox Content="Isolated" IsChecked="{Binding Content.IsIsolated}"/>
                    <CheckBox Content="Match Case" IsChecked="{Binding Content.MatchCase}"/>
                    <CheckBox Content="Include" IsChecked="{Binding Content.Include}"/>
                </StackPanel>
            </local:EditDeleteItem.Content>
        </local:EditDeleteItem>
</DataTemplate> 

我相信我需要使用命令绑定。 但是虽然我已经读了一两页,但是并不确定是将CommandBindings放在哪里,也不确定如何使用它们。

谢谢哈桑

事件被触发,但是您不会捕获它们,因为如果使用ItemTemplate,则Items_CollectionChanged中的订阅永远不会发生。

您应该了解ItemsControl(和ComboBox)如何与ItemsSource一起使用。 ItemsControl使用ItemContainerGenerator填充其可视树。 ItemsSource中的每个项目都包装到从ContentControl派生的容器中。 然后将item设置为Content,将ItemTemplate设置为ContentTemplate,依此类推。 当您将EditDeleteItem放入ItemTemplate时,它成为可视化树的一部分,而不是项目。 这就是为什么e.NewItems中没有EditDeleteItem且没有订阅的原因。

正如您提到的,正确的方法是命令。 您应该声明两个命令:

public class EditDeleteItem : UserControl
{
    ...
    public static readonly RoutedUICommand EditCommand = new RoutedUICommand(...);
    public static readonly RoutedUICommand DeleteCommand = new RoutedUICommand(...);
    ...
}

现在模板的一部分可能看起来像:

<WrapPanel ...>
    <Button ... Command="{x:Static EditDeleteItem.EditCommand}"/>
    <Button ... Command="{x:Static EditDeleteItem.DeleteCommand}"/>
</WrapPanel>

然后将命令绑定添加到dCB_Props:

public partial class dCB_Props : UserControl
{
    static dCB_Props()
    {
        ...
        CommandManager.RegisterClassCommandBinding(
            typeof(dCB_Props),
            new CommandBinding(EditDeleteItem.EditCommand, OnEditCommandExecuted));
        CommandManager.RegisterClassCommandBinding(
            typeof(dCB_Props),
            new CommandBinding(EditDeleteItem.DeleteCommand, OnDeleteCommandExecuted));
        ...
    }
    ...
}

您需要实现OnEditCommandExecuted和OnDeleteCommandExecuted才能处理EditDeleteItem中的相应命令。

希望我能正确理解您的问题;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM