ItemsControl类型的值无法添加到TimelineCollection类型的集合或字典中 [英] A value of type ItemsControl cannot be added to a collection or dictionary of type TimelineCollection

查看:95
本文介绍了ItemsControl类型的值无法添加到TimelineCollection类型的集合或字典中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我的目的是在点击现有按钮后生成一些新按钮。

我需要创建一个点击触发器。点击原点按钮后,我想添加包含新按钮的ItemsControl。

Hello, my intention is to generate some new buttons after click on already existing button.
I need to create a click trigger. After click on origin button i want to add there ItemsControl containing new buttons.

请有人知道我怎么能执行包含ItemsControl的触发器(生成新按钮,但这个生成对我有用) )。

Does please somebody have idea how could i execute trigger containing ItemsControl (generating new buttons, but this generating already works to me).

这是我的整个XAML,我在里面描述了关键代码部分:

Here is my whole XAML, i described the crucial code part inside:

<Window x:Class="EnterEventTextBox.DataView"

的xmlns = QUOT; HTTP://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

的xmlns:X =" HTTP://schemas.microsoft.com/winfx/2006/xaml"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

的xmlns:d =" HTTP://schemas.microsoft.com/expression/blend/2008"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

的xmlns:MC = QUOT; HTTP://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

的xmlns:本地= QUOT; CLR-名称空间:EnterEventTextBox"

xmlns:local="clr-namespace:EnterEventTextBox"

MC:可忽略= QUOT; d"背景= QUOT;黑色"

mc:Ignorable="d" Background="Black"

标题= QUOT;数据视图"高度= QUOT; 450"宽度= QUOT; 300"

Title="DataView" Height="450" Width="300"

的xmlns:I =" HTTP://schemas.microsoft.com/expression/2010/interactivity"

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

的xmlns:CAL = QUOT; HTTP://www.caliburnproject.org" >

xmlns:cal="http://www.caliburnproject.org" >


< Window.Resources>
< ItemsPanelTemplate x:Key =" ItemsPanelTemplate1">
< WrapPanel Orientation =" Horizo​​ntal" IsItemsHost = QUOT;真"背景= QUOT;绿松石" />
< / ItemsPanelTemplate>
< Style x:Key =" ItemsControlStyle1" TargetType =" {x:Type ItemsControl}">
< Setter Property =" Template">
< Setter.Value>
< ControlTemplate TargetType =" {x:Type ItemsControl}">
< Border BorderBrush =" {TemplateBinding BorderBrush}" BorderThickness =" {TemplateBinding BorderThickness}" Background =" {TemplateBinding Background}" Padding =" {TemplateBinding Padding}" SnapsToDevicePixels = QUOT;真">
< ScrollViewer CanContentScroll =" True" VerticalScrollBarVisibility = QUOT;可见" Horizo​​ntalScrollBarVisibility = QUOT;可见">
< ItemsPresenter SnapsToDevicePixels =" {TemplateBinding SnapsToDevicePixels}" />
< / ScrollViewer>
< / Border>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< / Style>
< /Window.Resources>
< Grid>
< Grid.RowDefinitions>
< RowDefinition Height =" Auto" />
< RowDefinition Height =" Auto" />
< RowDefinition Height =" *" />
< /Grid.RowDefinitions>
< ItemsControl ItemsSource =" {Binding Shippers}" ItemsPanel =" {DynamicResource ItemsPanelTemplate1}" Style =" {DynamicResource ItemsControlStyle1}">
< ItemsControl.ItemTemplate>
< DataTemplate DataType =" {x:Type Button}">
< WrapPanel Background =" Green"取向= QUOT;水平">
< Button Height =" 50"宽度= QUOT; 50"背景= QUOT;红色" Content =" {Binding BtnLabelShipper}"余量= QUOT; 0,0,5,5">
<! - 这里我试图设置触发器,但它不工作并抛出它错误写在标题 - >
< Button.Triggers>
< EventTrigger RoutedEvent =" Button.Click">
< EventTrigger.Actions>
< BeginStoryboard>
< Storyboard>

<! - 我需要在触发器被触发时执行>
< ItemsControl ItemsSource =" {Binding Parcels}">
< ItemsControl.ItemTemplate>
< DataTemplate DataType =" {x:Type Button}">

< Button Height =" 50"宽度= QUOT; 50"背景= QUOT;红色" Content =" {Binding ParcelNumber}"余量= QUOT; 0,0,5,5" />
< / DataTemplate>
< /ItemsControl.ItemTemplate>
< / ItemsControl>
- >
< / Storyboard>
< / BeginStoryboard>
< /EventTrigger.Actions>
< / EventTrigger>
< /Button.Triggers>
< / Button>
< / WrapPanel>
< / DataTemplate>
< /ItemsControl.ItemTemplate>
< / ItemsControl>
< / Grid>
< / Window>

<Window.Resources> <ItemsPanelTemplate x:Key="ItemsPanelTemplate1"> <WrapPanel Orientation="Horizontal" IsItemsHost="True" Background="Turquoise"/> </ItemsPanelTemplate> <Style x:Key="ItemsControlStyle1" TargetType="{x:Type ItemsControl}"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type ItemsControl}"> <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"> <ScrollViewer CanContentScroll="True" VerticalScrollBarVisibility="Visible" HorizontalScrollBarVisibility="Visible"> <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/> </ScrollViewer> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <ItemsControl ItemsSource="{Binding Shippers}" ItemsPanel="{DynamicResource ItemsPanelTemplate1}" Style="{DynamicResource ItemsControlStyle1}"> <ItemsControl.ItemTemplate> <DataTemplate DataType="{x:Type Button}"> <WrapPanel Background="Green" Orientation="Horizontal"> <Button Height="50" Width="50" Background="Red" Content="{Binding BtnLabelShipper}" Margin="0,0,5,5"> <!--HERE I TRIED TO SET TRIGGER, BUT IT DOES NOT WORK and throws it error wrote in headline--> <Button.Triggers> <EventTrigger RoutedEvent="Button.Click"> <EventTrigger.Actions> <BeginStoryboard> <Storyboard> <!-- THIS I NEED TO EXECUTE WHEN TRIGGER IS FIRED UP> <ItemsControl ItemsSource="{Binding Parcels}"> <ItemsControl.ItemTemplate> <DataTemplate DataType="{x:Type Button}"> <Button Height="50" Width="50" Background="Red" Content="{Binding ParcelNumber}" Margin="0,0,5,5"/> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> --> </Storyboard> </BeginStoryboard> </EventTrigger.Actions> </EventTrigger> </Button.Triggers> </Button> </WrapPanel> </DataTemplate> </ItemsControl.ItemTemplate> </ItemsControl> </Grid> </Window>




$




推荐答案

嗨RadekJak,

Hi RadekJak,

根据你的描述,你想点击按钮添加收藏品,对吗?如果是,您可以查看以下代码,请在项目中添加这些命名空间:

According to your description, you want to click button to add item for collection, am I right? If yes, you can take a look the following code, please add these namespaces in your project:

xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:command1="http://www.galasoft.ch/mvvmlight"

您可以通过NuGet Package下载...

You can download by NuGet Package...

 <Grid>
        <ItemsControl ItemsSource="{Binding Shippers}">
            <ItemsControl.ItemTemplate>
                <DataTemplate DataType="{x:Type Button}">
                    <WrapPanel Background="Green" Orientation="Horizontal">
                        <Button
                            Width="50"
                            Height="50"
                            Margin="0,0,5,5"
                            Background="Red"
                            Content="{Binding BtnLabelShipper}">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Click">
                                    <command1:EventToCommand Command="{Binding RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}, Path=DataContext.buttonclick}" />

                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </Button>
                    </WrapPanel>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Grid>


  public partial class Window1 : Window
    {
        public ObservableCollection<Shipper> Shippers { get; set; }
        public RelayCommand buttonclick { get; set; }
        public Window1()
        {
            InitializeComponent();
            Shippers = new ObservableCollection<Shipper>()
            {
                new Shipper(){BtnLabelShipper="test1"},
                new Shipper(){BtnLabelShipper="test2"},
                new Shipper(){BtnLabelShipper="test3"}
            };
            buttonclick = new RelayCommand(click);
            this.DataContext = this;
        }
        public void click()
        {
            MessageBox.Show("OK!");
            Shippers.Add(new Shipper() { BtnLabelShipper = "test4" });
        }
    }
    public class Shipper
    {
        public string BtnLabelShipper { get; set; }
    }

最好的问候,

Cherry


这篇关于ItemsControl类型的值无法添加到TimelineCollection类型的集合或字典中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆