简体   繁体   中英

When is label.Target property set when using Elementbinding?

I have a small testview in Silverlight which consists of the following grid:

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <Controls:Label x:Name="label" Content="LabelContent" Target="{Binding ElementName=textBox}" />
    <TextBox x:Name="textBox" Text="Foobar" Grid.Column="1" />
</Grid>

Not sure what I'm doing wrong here, but it doesn't seem to set the target property on the label control. I expected to find a reference to the textbox in label.Target after InitializeComponent() have been invoked in the constructor for the view, but it is still null.

Any idea what I might be missing here?

EDIT: A little clarification here. This view is initialized from code and never in it self part of any visible view. It's part of a search feature where I search for controls matching some criteria and put them on a search result page. In this case, if the label matches the criteria it should be part of the search result together with the associated control based on the target property. Not sure when element binding is performed, but so far it looks like it don't bind soon enough (or at all without belonging to a visual tree?).

The problem is that during the constructor data binding is not guaranteed to have been carried out. Try testing it in the view's loaded event instead. Eg:-

  InitializeComponent()
  Loaded += (s, args) => { MessageBox.Show((label.Target != null).ToString()); }

As far as I can conclude, Elementbinding is not supported when simply initializing a view without adding it to a parent view. Other bindings work, but not elementbinding :(

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