简体   繁体   中英

x:Bind not binding to attached property

Can anybody chime in how this is done correctly? I get a compiler error in the generated code behind.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="100"/>
        <RowDefinition Height="100"/>
    </Grid.RowDefinitions>
    <Rectangle x:Name="BlueRect" Width="100" Height="100" Grid.Row="1" Fill="Blue" />
    <Rectangle Width="100" Height="100" Grid.Row="{x:Bind BlueRect.(Grid.Row),Mode=OneWay}" Fill="Red" />
</Grid>

(this is just a minimal example, without the Mode=OneWay it obviously works, but I need that... Also, I'd like to do this with {x:Bind} not the traditional {Binding} )

使用元素名称绑定:

<Rectangle Width="100" Height="100" Grid.Row="{Binding ElementName=BlueRect,Path=(Grid.Row),Mode=OneWay}" Fill="Red" />

You can report this on Visual Studio connect .

I think you can consider work around this by calling this.Bindings.Update() when you change the row and keep x:bind OneTime.

For example, we may change the Grid.Row for "BlueRect" in a button click, and the following code shows how we can handle this scenario.

In XAML:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="100"/>
        <RowDefinition Height="100"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Rectangle x:Name="BlueRect" Width="100" Height="100" Grid.Row="1" Fill="Blue" />
    <Rectangle Width="100" Height="100" Grid.Row="{x:Bind BlueRect.(Grid.Row)}" Fill="Red" />
    <Button Content="Click me" Grid.Row="3" Click="Button_Click"></Button>
</Grid>

Code Behind:

private void Button_Click(object sender, RoutedEventArgs e)
{
    Grid.SetRow(BlueRect, 2);
    this.Bindings.Update();
}

this.Bindings.Update() can also be used when the data is loaded async, and the data may not be ready before x:bind be initialized.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM