Silverlight EventTrigger在MVVM中的ItemsControl内部不工作
我想在MVVM中动态绑定多个按钮。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>
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组件中删除双向绑定模式?