0

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

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" />

是什么导致异常?

4

1 回答 1

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!

于 2013-10-06T19:36:54.177 回答