ListBox element returns conflicting property for children

So, now I’m at an impasse, and I hope that another pair of eyes will catch what I’m missing, or at least point me to the side to dig further. Sorry in advance, I do not often ask quick and easy questions.

Scenario: A huge Silverlight solution with some erratic details that they want to use with new features, but rewrites or something else without much effort. One of which is a virtualized ListBox. This ListBox is the parent of many different things in ways that I usually did not expect to see as they were. So, to visualize a little hierarchy in pseudo;

ListBox (parent)

-ListBoxItemTemplate - The user control that hosts the ContentControl, where other UserControls are unloaded.

--- Several other UserControls that are unloaded in the previous ContentControl at runtime based on numerous conditions.

---- Inside each nested UserControl, other nested ItemsControls and collections with their own Item template are often used.

I hope I still have your understanding of this monster, but if not, there is something more visual here to help convey the problem I am addressing; enter image description here

So here is my current problem. Based on what they want, without doing a lot of refactoring, I try to make lemonade and use the SelectedItem bool from the parent list to perform some actions in some of these UserControls subtasks.

IsSelected ListBoxItem. (xaml wise) {Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBoxItem}} UserControls.

, , True/False , , . , UserControls , True IsSelected UserControl, False - UserControls. . , .

, 20 . . , , 18 True, , , 2 False. , ListBoxItem. , , , . , ListBoxItem , , .... .

. , , . , , . , , , . , , - Focused , , , bool True, False.

, . , , , ? - ( )?

ListBox, - . , ? , Silverlight? , - - ? , , , , . , .

, , - , , , , -.:)

+4
2

- ( ) - , , ( ).

enter image description here

: BindableObjectReference ItemTemplate ItemContainerStyle.Template . , , .

<ListBox.ItemTemplate>
    <DataTemplate>
        <Grid...>
          <Grid.Resources>
            <BindableObjectReference x:Key="TopLevelListBoxItem"
              Object="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}}"/>
          </Grid.Resources>
          ...
        </Grid>
    </DataTemplate>
</ListBox.ItemTemplate>


<MyNestedSubControl
  ...
  Foo="{Binding Path=Object.IsSelected, Source={StaticResource TopLevelListBoxItem}}"
  .../>

BindableObjectReference:

public class BindableObjectReference : DependencyObject
{
    public object Object
    {
        get { return GetValue( ObjectProperty ); }
        set { SetValue( ObjectProperty, value ); }
    }

    public static readonly DependencyProperty ObjectProperty =
        DependencyProperty.Register( "Object", typeof( object ),
        typeof( BindableObjectReference ), new PropertyMetadata( null ) );
}

: . , .

+2

.

FindAncestor, AncestorType . , ListBoxItem ComboBoxItem ListViewItem, , .

0

Source: https://habr.com/ru/post/1618164/


All Articles