none
Binding issue to Parent datacontext RRS feed

  • Question

  • Dear all,

    I have the following defgineition of a listbox :

    <ListBox x:Name="_listofCharts" Grid.Row="3" Grid.ColumnSpan="3" 
                     ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
    				 ScrollViewer.CanContentScroll="False" Loaded="_listofCharts_Loaded"
    				 ItemsSource="{Binding ChartViewModels,UpdateSourceTrigger=PropertyChanged}"
    				 HorizontalAlignment="Stretch" 
    				 Background="{DynamicResource WindowBackgroundBrush}"
    				 SelectedItem="{Binding SelectedChartItem,Mode=TwoWay}"
    				 Style="{StaticResource CompareChartList}">
    
    				<ListBox.ItemContainerStyle>
    					<Style TargetType="ListBoxItem">
    						<Setter Property="HorizontalContentAlignment" Value="Stretch" />
    					<Setter Property="IsSelected" Value="{Binding IsChartSelected}" />
    					<Setter Property="Padding" Value="0"/>
    					<Setter Property="Template">
    						<Setter.Value>
    							<ControlTemplate TargetType="{x:Type ListBoxItem}">
    								<ContentPresenter/>
    							</ControlTemplate>
    						</Setter.Value>
    					</Setter>
    
    				</Style>
    				</ListBox.ItemContainerStyle>
    
    			</ListBox>

    I have a list of IstBox items which are based on a DatatTemplate. IN this datatemplate I have an obkect to which I need to bind a Property to the Datacontext of the Listbox as below :

    <s:HorizontalLineAnnotation x:Name="_sigmaHight" HorizontalAlignment="Stretch"
                 LabelPlacement="Axis"  Panel.ZIndex="4"
                 LabelTextFormatting="0.00" ShowLabel="True"
                 Style="{DynamicResource SigmaAnnotationStyle}"
    	     IsHidden="{Binding RelativeSource={RelativeSource FindAncestor
    		,AncestorType={x:Type ListBox}},Path=DataContext.HideSigmaCursor,UpdateSourceTrigger=PropertyChanged}"/>

    The property DataContext.HideSigmaCursor is available on the LIstBox Datacontext, but doing this seems the binding does not occurs.

    Did I do something wrong ?

    Thnaks for advise

    Friday, April 27, 2018 12:29 PM

Answers

  • Hi wakefun,

    >>The property DataContext.HideSigmaCursor is available on the LIstBox Datacontext, but doing this seems the binding does not occurs.

    Have you verify the following things?

    1. The DataContext

    2. The HideSigmaCursor property definition and value assignment. I would suggestion adding a TextBlock and set Text binding to check the value inside the DataTemplate.

    3. Check if this is an issue of third-party control. I found you are using a 3rd-party chart control.

    I made a quick test and share with you:

    XAML:

    <Window.Resources>
            <DataTemplate x:Key="postBody">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=Author}" />
                    <Button Margin="10,0,0,0" IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}, 
                        Path=DataContext.HideSigmaCursor, 
                        UpdateSourceTrigger=PropertyChanged}" >Click</Button>
                    <TextBlock Margin="10,0,0,0" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}, 
                        Path=DataContext.HideSigmaCursor, 
                        UpdateSourceTrigger=PropertyChanged}" />
                </StackPanel>
            </DataTemplate>
        </Window.Resources>
        <Grid>
            <ListBox ItemTemplate="{StaticResource postBody}" ItemsSource="{Binding Posts}" />
        </Grid>

    Code:

    public partial class MainWindow : Window
        {
            public List<Post> Posts { get; set; }
            public Boolean HideSigmaCursor { get; set; }
            public MainWindow()
            {
                InitializeComponent();
                Posts = new List<Post>();
                for (int i = 1; i<11; i++)
                    Posts.Add( new Post() { Author = $"person{i}" } );
                HideSigmaCursor = false;
                this.DataContext = this;
            }
        }
    
        public class Post
        {
            public string Author { get; set; }
        }
    

    Screenshot:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by wakefun Tuesday, May 1, 2018 11:52 AM
    Monday, April 30, 2018 2:42 AM
    Moderator

All replies

  • Hi wakefun,

    >>The property DataContext.HideSigmaCursor is available on the LIstBox Datacontext, but doing this seems the binding does not occurs.

    Have you verify the following things?

    1. The DataContext

    2. The HideSigmaCursor property definition and value assignment. I would suggestion adding a TextBlock and set Text binding to check the value inside the DataTemplate.

    3. Check if this is an issue of third-party control. I found you are using a 3rd-party chart control.

    I made a quick test and share with you:

    XAML:

    <Window.Resources>
            <DataTemplate x:Key="postBody">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=Author}" />
                    <Button Margin="10,0,0,0" IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}, 
                        Path=DataContext.HideSigmaCursor, 
                        UpdateSourceTrigger=PropertyChanged}" >Click</Button>
                    <TextBlock Margin="10,0,0,0" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBox}}, 
                        Path=DataContext.HideSigmaCursor, 
                        UpdateSourceTrigger=PropertyChanged}" />
                </StackPanel>
            </DataTemplate>
        </Window.Resources>
        <Grid>
            <ListBox ItemTemplate="{StaticResource postBody}" ItemsSource="{Binding Posts}" />
        </Grid>

    Code:

    public partial class MainWindow : Window
        {
            public List<Post> Posts { get; set; }
            public Boolean HideSigmaCursor { get; set; }
            public MainWindow()
            {
                InitializeComponent();
                Posts = new List<Post>();
                for (int i = 1; i<11; i++)
                    Posts.Add( new Post() { Author = $"person{i}" } );
                HideSigmaCursor = false;
                this.DataContext = this;
            }
        }
    
        public class Post
        {
            public string Author { get; set; }
        }
    

    Screenshot:


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by wakefun Tuesday, May 1, 2018 11:52 AM
    Monday, April 30, 2018 2:42 AM
    Moderator
  • More information :

    My Root DataContext is named MainViewModel which contains a property named ChartViewModels

    If you look at my ListBox it as its ItemSource property set to a collection of ChartViewModels which is a collection of ChartViewModel:

    • While the ListBox gets loaded if I check the datacontext it is set to : MainViewModel which is correct

    Each ChartViewModel items from ListBox ItemSource has a property HideSigmaCursor, which I guess should be directly accessible from the Datatemplate but as the DatContext of the ListBox is pointing to MainViewModel it seems the context is wrong

    How can I set the binding of IsHidden property to ChartViewModel in order that it point to the actual ChartViewModel ?

    regards 



    • Edited by wakefun Tuesday, May 1, 2018 8:57 AM
    Tuesday, May 1, 2018 8:22 AM
  • I find out the issue.

    You debugging tricks help to find out that I was at a root context

    thks 

    Tuesday, May 1, 2018 11:52 AM