如何将项目点击事件添加到项目控件

时间:2015-05-11 18:38:13

标签: c# wpf xaml

我在为item控件中的项生成click事件时遇到问题。我是xaml nad wpf的新手。各位专家能帮助我吗?下面是我提出的代码,但现在不知道如何为生成的项添加click事件。非常感谢您的回复。感谢您阅读

<ItemsControl ItemsSource="{Binding Text, Source={StaticResource TextContainer}}">
            <!--text is an object bein made public from TextToDisplay. There can be many objects released ratger than one in this case-->
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel ItemWidth="100"
                               ItemHeight="100" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="{x:Type sys:String}">
                    <Border CornerRadius="100"
                            Background="BlueViolet">
                        <Button Margin="20"
                                   Content="{Binding}"
                                   HorizontalContentAlignment="Center" />
                    </Border>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

1 个答案:

答案 0 :(得分:0)

你可以做的最简单的事情就是创建你自己的用户控件......把它变成一个自定义按钮并将click事件添加到该控件中,这样每次listbox添加你的控件时它都会内置一个click_event。

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        // add your code here.
    }

然后对于列表框做类似的事情......

    private void AddItemsButton_Click(object sender, RoutedEventArgs e)
    {

        ListBoxTest.Items.Add(new UserControl1());
    }
每当您点击标题为&#34;添加项目&#34;

的按钮时,

就会向列表框添加一个usercontrol按钮