因爲我在一個ItemsControl中移動了View,所以無法在ViewModel中觸發EventTrigger。這裏是我的條命令宣言應該開槍引發LostFocus:ItemsControl + Textbox + MVVM + EventTrigger Not Firing
public ICommand UpdateOrdersCommand { get; set; }
public void UpdateOrders(object param)
{
UpdateFields();
}
我的容器XAML:
<UserControl.Resources>
<WMS:PurchasingModel x:Key="ViewModel" />
<DataTemplate x:Key="myDataTemplate1">
<WMSViews:Purchasing1 />
</DataTemplate>
</UserControl.Resources>
<Grid x:Name="LayoutRoot"
Background="White"
DataContext="{StaticResource ViewModel}">
<ItemsControl ItemsSource="{Binding Path=ColPurchasing}"
ItemTemplate="{StaticResource myDataTemplate1}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
我的視圖,其中該EvenTrigger用來開火引發LostFocus:
<Classes:TextBoxEx TabIndex="1"
x:Name="tbFoodSales"
HorizontalAlignment="Left"
Height="23"
Margin="55,79,0,0"
TextWrapping="Wrap"
VerticalAlignment="Top"
Width="61"
TextAlignment="Right"
Text="{Binding Path=FoodSales, Mode=TwoWay}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="LostFocus">
<i:InvokeCommandAction Command="{Binding UpdateOrdersCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Classes:TextBoxEx>
我使用Silverlight!這沒有用。輸出窗口是一個有用的提示。它現在顯示:System.Windows.Data錯誤:BindingExpression路徑錯誤:'WMS.WCF_WingService.Purchasing''WMS.WCF_WingService.Purchasing'(HashCode = 53022201)上未找到'UpdateOrdersCommand'屬性。 BindingExpression:Path ='DataContext.UpdateOrdersCommand'DataItem ='System.Windows.Controls.Grid – Colbs 2013-02-14 06:16:52
嘗試給你的ItemsControl一個名稱,然後在命令綁定中使用該名稱作爲ElementName。這有點冒險,但它需要對當前代碼進行最少量的更改。 – wdavo 2013-02-14 06:25:12
仍然沒有事件發射。它正在尋找我的視圖的命令,而不是我的ViewModel ..數據項或查找祖先的工作?我試過無盡的變化 – Colbs 2013-02-14 06:39:18