silverlight - 为什么 silverlight 中的双向绑定(bind)不起作用?

标签 silverlight data-binding two-way-binding

根据 Silverlight TwoWay 绑定(bind)的工作原理,当我更改 FirstName 字段中的数据时,它应该更改 CheckFirstName 字段中的值。

为什么不是这样?

回答:

谢谢 Jeff,就是这样,对于其他人:这里是 the full solution with downloadable code .

XAML:

<StackPanel>
    <Grid x:Name="GridCustomerDetails">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="300"/>
        </Grid.ColumnDefinitions>

        <TextBlock VerticalAlignment="Center" Margin="10" Grid.Row="0" Grid.Column="0">First Name:</TextBlock>
        <TextBox Margin="10" Grid.Row="0" Grid.Column="1" Text="{Binding FirstName, Mode=TwoWay}"/>

        <TextBlock VerticalAlignment="Center" Margin="10" Grid.Row="1" Grid.Column="0">Last Name:</TextBlock>
        <TextBox Margin="10" Grid.Row="1" Grid.Column="1" Text="{Binding LastName}"/>

        <TextBlock VerticalAlignment="Center" Margin="10" Grid.Row="2" Grid.Column="0">Address:</TextBlock>
        <TextBox Margin="10" Grid.Row="2" Grid.Column="1" Text="{Binding Address}"/>

    </Grid>

    <Border Background="Tan" Margin="10">
        <TextBlock x:Name="CheckFirstName"/>
    </Border>

</StackPanel>

代码隐藏:

public Page()
{
    InitializeComponent();

    Customer customer = new Customer();
    customer.FirstName = "Jim";
    customer.LastName = "Taylor";
    customer.Address = "72384 South Northern Blvd.";

    GridCustomerDetails.DataContext = customer;

    Customer customerOutput = (Customer)GridCustomerDetails.DataContext;
    CheckFirstName.Text = customer.FirstName;

}

最佳答案

您的 Customer 类型必须支持 INotifyPropertyChanged为了让绑定(bind)知道您的 FirstName 属性值何时更改。

tutorial可能会帮助您使代码正常工作。

关于silverlight - 为什么 silverlight 中的双向绑定(bind)不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/553521/

相关文章:

c# - 高效地将音频字节 - byte[] 转换为 Short[]

javascript - 将选项值绑定(bind)到 AngularJS 中的 ng-model

wpf - 如何在 FlowDocument 中隐藏段落?

javascript - Controller 和外部指令之间的两种方式数据绑定(bind)

c# - "Please Select"的数据绑定(bind)下拉列表不起作用

javascript - 处理 Angular 5 组件中的模型更改(双向绑定(bind))

c# - Windows 窗体组合框中的复杂数据绑定(bind)

silverlight - Silverlight MVVM隔离存储

c# - 为什么我不能在 Silverlight 中绑定(bind) Grid.RowDefinition 高度?

c# - LINQ 实体结果到 List<string>