Why is two-way binding in silverlight not working?

随声附和 提交于 2019-12-11 02:54:54

问题


According to how Silverlight TwoWay binding works, when I change the data in the FirstName field, it should change the value in CheckFirstName field.

Why is this not the case?

ANSWER:

Thank you Jeff, that was it, for others: here is 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>

Code behind:

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;

}

回答1:


Your Customer type has to support INotifyPropertyChanged in order for the binding to know when your FirstName property value has changed.

This tutorial may help you in getting your code to work.




回答2:


Your controls inside of the Grid container have no idea what FirstName, LastName, and Address are. I think that you need to bind your grid to the object in the codebehind:

<Grid x:Name="GridCustomerDetails" DataContext="Customer"> 

Now every control inside of that container can be bound to a property of Customer. You bind it like so:

<TextBox Margin="10" Grid.Row="0" Grid.Column="1" 
         Text="{Binding Path=FirstName, Mode=TwoWay}"/>   

In your code behind, make sure that "Customer" is a class object, and is declared publicly.

If this doesn't work, try adding an x:Name="" to the page declarations and namespaces at the top.

I hope this helps!




回答3:


solution is use element binding for CheckFirstName



来源:https://stackoverflow.com/questions/553521/why-is-two-way-binding-in-silverlight-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!