Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Silverlight EventTrigger在MVVM中的ItemsControl内部不工作_Silverlight_Silverlight 4.0_Mvvm - Fatal编程技术网

Silverlight EventTrigger在MVVM中的ItemsControl内部不工作

Silverlight EventTrigger在MVVM中的ItemsControl内部不工作,silverlight,silverlight-4.0,mvvm,Silverlight,Silverlight 4.0,Mvvm,我想在MVVM中动态绑定多个按钮。 1.我使用ItemControl动态创建了按钮 2.它没有调用触发器单击事件。 请帮我做这个 <ItemsControl ItemsSource="{Binding ComponentList,Mode=TwoWay}"> <ItemsControl.ItemTemplate> <DataTemplate>

我想在MVVM中动态绑定多个按钮。
1.我使用ItemControl动态创建了按钮
2.它没有调用触发器单击事件。 请帮我做这个

        <ItemsControl ItemsSource="{Binding ComponentList,Mode=TwoWay}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Button Tag="{Binding WorkFlowCompId}">
                                <Button.Content>
                                    <TextBlock Text="{Binding ComponentName,Mode=TwoWay}"/>
                                </Button.Content>
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Click">
                                        <i:InvokeCommandAction Command="{Binding ComponentSelected}" 
CommandParameter="{Binding WorkFlowCompId,Mode=TwoWay}" >
                                        </i:InvokeCommandAction>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </Button>
                        </StackPanel>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>

第一次通过Xaml应该是什么样子:-

        <ItemsControl ItemsSource="{Binding ComponentList}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Command="{Binding SelectComponent}">
                        <TextBlock Text="{Binding ComponentName}"/>
                    </Button>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

正如Derek在评论中提到的,我怀疑容器上有一个
ComponentSelected
命令。但是,您应该将此命令移动到零部件的视图模型上。注意,我还将其重命名为
SelectComponent
,因此这听起来像是一个操作,而不是一个属性

双向绑定已被删除,在这种情况下它不会做任何事情。从一个简单的绑定中分配一个标记值应该会给设计带来一些问题


顺便说一句,由于您正在进行某种形式的选择,
列表框
在这种情况下是否更合适?

您的问题是命令从其模板中获取上下文,因此无法访问ViewModel的根。将此类添加到您的解决方案中:

public class DataContextProxy : FrameworkElement
    {
        public DataContextProxy()
        {
            this.Loaded += new RoutedEventHandler(DataContextProxyLoaded);
        }

        void DataContextProxyLoaded(object sender, RoutedEventArgs e)
        {
            Binding binding = new Binding();
            if (!String.IsNullOrEmpty(BindingPropertyName))
            {
                binding.Path = new PropertyPath(BindingPropertyName);
            }
            binding.Source = this.DataContext;
            binding.Mode = BindingMode;
            this.SetBinding(DataContextProxy.DataSourceProperty, binding);
        }

        public Object DataSource
        {
            get { return (Object)GetValue(DataSourceProperty); }
            set { SetValue(DataSourceProperty, value); }
        }

        public static readonly DependencyProperty DataSourceProperty =
            DependencyProperty.Register("DataSource", typeof(Object), typeof(DataContextProxy), null);


        public string BindingPropertyName { get; set; }

        public BindingMode BindingMode { get; set; }

    }
然后在XAML中使用它,如下所示:

 <UserControl.Resources>
            <library:DataContextProxy x:Key="DataContextProxy"/>
    </UserControl.Resources>

然后在命令绑定中:

<Button Tag="{Binding WorkFlowCompId}">
                                <Button.Content>
                                    <TextBlock Text="{Binding ComponentName,Mode=TwoWay}"/>
                                </Button.Content>
                                <i:Interaction.Triggers>
                                    <i:EventTrigger EventName="Click">
                                        <i:InvokeCommandAction Command="{Binding DataSource.ComponentSelected, Source={StaticResource DataContextProxy}" 
CommandParameter="{Binding WorkFlowCompId,Mode=TwoWay}" >
                                        </i:InvokeCommandAction>
                                    </i:EventTrigger>
                                </i:Interaction.Triggers>
                            </Button>


这是什么i:Interaction.Triggers?xmlns:i=“”我正在使用Microsoft.Expression.Interactions进行内部事务处理,因此您可能需要在问题中指定它。代码一看就对了。尝试检查命令是否绑定到InvokeCommandAction,并从项或父项上选择的ItemsSourceIs组件中删除双向绑定模式?