wpf customcontrol,如何绑定按钮单击itemscontrol DataTemplate

时间:2015-04-26 18:24:24

标签: c# wpf xaml wpf-controls

我有继承自Textbox的自定义控件,在.cs中我有DependencyProperty SelectedItems(如果有的话,那些项目将在texbox中显示)如果不是texbox看起来像普通的texbox。

在模板中的generic.xaml我添加了新的边框,里面我把ItemsControl和项目模板放在一起

xaml itemControl部分:

<Border BorderBrush="Green" BorderThickness="1">
                        <ItemsControl x:Name="PART_SelectedItemsHost"
                                ItemsSource="{TemplateBinding SelectedItems}"
                                ItemTemplate="{TemplateBinding SelectedItemsTemplate}"
                                VerticalAlignment="Stretch"
                                HorizontalAlignment="Stretch"                                                                      
                                Margin="{TemplateBinding Padding}"
                                Visibility="Visible">
                            <ItemsControl.ItemsPanel>
                                <ItemsPanelTemplate>
                                    <WrapPanel IsItemsHost="True">
                                    </WrapPanel>
                                </ItemsPanelTemplate>
                            </ItemsControl.ItemsPanel>                               
                        </ItemsControl>                            
                    </Border>

Xaml ItemTemplate部分:

<DataTemplate x:Key="DefaultSelectedItemsTemplate" >
    <Border x:Name="selectedItemBorder" BorderBrush="Gray" BorderThickness="1" CornerRadius="5" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" Margin="5,1,1,1">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="15"/>
            </Grid.ColumnDefinitions>
            <TextBlock Grid.Column="0" Text="{Binding}" Margin="5,0,3,0"></TextBlock>
            <Button BorderThickness="0" Grid.Column="1" Click="???" or Command="???" >X</Button>
        </Grid>
    </Border>
</DataTemplate>

现在我怎么能绑定那个按钮&#34; X&#34;在我的.cs文件中..我正在尝试这样做覆盖OnApplyTemplate()方法...而我确实设法绑定mousedown当我点击项目texbox但我不知道如何附加点击按钮

我的.cs部分

            public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        ItemsControl itmControl = GetTemplateChild("PART_SelectedItemsHost") as ItemsControl;

        if (itmControl != null)
        {
            itmControl.MouseLeftButtonDown += new MouseButtonEventHandler(itmControl_MouseLeftButtonDown);

            // blind click on X buttons in ItemsControl
        }

    }

    private void itmControl_MouseLeftButtonDown(object obj, MouseButtonEventArgs e)
    {
        //IsMouseLeftButtonDown = true;
        System.Diagnostics.Debug.WriteLine(e.OriginalSource.ToString());
        object item = (e.OriginalSource as FrameworkElement).DataContext;
        deleteSelectedItem(item);

    }

这里是图片,这个控件看起来像是X按钮需要从SelectedItem集合中删除项目的蚂蚁。

picture

1 个答案:

答案 0 :(得分:1)

ItemsControl具有以下名称:

<ItemsControl x:Name="PART_SelectedItemsHost"

所以在模板中

<Button  BorderThickness="0" Grid.Column="1" Command="{Binding DataContext.DeleteItem, ElementName=PART_SelectedItemsHost}" CommandParameter="{Binding}">X</Button>