2015-05-11 213 views
0

我在为物品控件中的物品生成点击事件时遇到问题。我是xaml nad wpf的新手。你能帮我解决问题吗?下面是我已经提出的代码,但现在不知道如何为生成的项目添加单击事件。非常感谢你的回应。感谢您阅读如何将物品点击事件添加到物品控件

<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> 
+0

........... ........

+0

您将不得不编辑您的曲目并在那里发布代码。评论代码是完全不可读的。 – goobering

+0

也许一个'ListBox'或者已经处理过Item事件的东西会更适合你的目的,否则你可以把Button或任何你喜欢的东西放在ItemsControl中,还有其他的信息可以指向你正确的方向。 –

回答

0

或许你能做的就是创建自己的用户控制......使之成为自定义按钮,然后单击事件添加到该控件的最简单的事情所以每次在列表框中添加控件会已经有一个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()); 
    } 

这增加了用户控件按钮,在列表框中每次你点击标题为 “添加项”