Binding.UpdateSourceTrigger在WPF中给出XamlParseException / TargetInvocationException

时间:2013-10-06 19:19:42

标签: c# wpf xamlparseexception updatesourcetrigger targetinvocationexception

我正在尝试按如下方式应用触发器:

using SmartRoutePlanner.Models;
...
Map locationMap = new Map();

locationTextBox.DataContext = locationMap;

Binding locationBinding = new Binding("Location");
locationTextBox.SetBinding(TextBox.TextProperty, locationBinding);
locationBinding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;

我的XAML代码是这样的:

...
xmlns:models="clr-namespace:SmartRoutePlanner.Models"
...
<Grid.Resources>
    <models:Map x:Key="mapDataSource"/>
</Grid.Resources>
<Grid.DataContext>
    <Binding Source="{StaticResource mapDataSource}" />
</Grid.DataContext>
<TextBox x:Name="locationTextBox" />

造成例外的原因是什么?

1 个答案:

答案 0 :(得分:0)

在XAML中,它应该如下所示:

<Window x:Class="Teste.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <TextBox Text="{Binding PropertyFromDataContext, UpdateSourceTrigger=Explicit}" />
    </Grid>
</Window>

请记住,您绑定了DataContext

中的属性