locked
How can I bind to a property on CodeBehind from ListBox item Datatemplate? RRS feed

  • Question

  • Hi,

    I have a list box and items are defined by a DataTemplate such as

     <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Vertical" >
                                <TextBlock TextWrapping="Wrap" x:Name="txtAnnotation" 
    Width="{Binding Source= ???????????, Path=TextLineWidth}" Text="{Binding Path=LayoutAnnotation.ANNOTATION.ANNOTATION1}" /> <StackPanel Orientation="Horizontal" Margin="10,0,0,0"> <TextBlock Width="70" Style="{StaticResource TBDateTimeStamp }" Text="{Binding Path=LayoutAnnotation.ANNOTATION.CREATED_ON}" /> <Button Margin="5,0,0,0" VerticalAlignment="Top"
    Style="{StaticResource closeButtonStyle }" x:Name="btnRemove" Click="btnRemove_Click" Visibility="{Binding Path=DeleteVisible}" /> </StackPanel> </StackPanel> </DataTemplate>
    </ListBox.ItemTemplate>

    By default, Bindings in this datatemplate reference the ItemSource of the ListBox.  But, for the width (see lTextBlock on ine 4) I want to reference a property in my codebehind.  How can I specify the source so that it uses my CodeBehind instead of the ListBox's ItemSource?

    Thanks,

    Jerry

    Thursday, May 19, 2011 12:43 PM

Answers

  • try:
     
    <UserControl ... Name="myPage">
    ...
     <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Vertical" >
                                <TextBlock TextWrapping="Wrap" x:Name="txtAnnotation"
                                            Width="{Binding ElementName=myPage, Path=TextLineWidth}"
                                          Text="{Binding Path=LayoutAnnotation.ANNOTATION.ANNOTATION1}" />
                                <StackPanel Orientation="Horizontal" Margin="10,0,0,0">
                                    <TextBlock Width="70"  Style="{StaticResource TBDateTimeStamp }"
                                            Text="{Binding Path=LayoutAnnotation.ANNOTATION.CREATED_ON}" />
                                    <Button Margin="5,0,0,0" VerticalAlignment="Top"
                                            Style="{StaticResource closeButtonStyle }" x:Name="btnRemove" Click="btnRemove_Click"  
                                        Visibility="{Binding Path=DeleteVisible}" />
                                </StackPanel>
                            </StackPanel>
                        </DataTemplate>
    </ListBox.ItemTemplate>

    Thursday, May 19, 2011 2:12 PM
  • You can bind it to some other control Width, for instance:


    <TextBlock TextWrapping="Wrap" x:Name="txtAnnotation" 
                                            Width="{Binding Width,ElementName=pattern}"
                                          Text="{Binding Path=LayoutAnnotation.ANNOTATION.ANNOTATION1}" />


    To your xaml add element with name "pattern" for instance:

    <Grid x:Name="pattern" Loaded="Grid_Loaded" Width="{Binding TextLineWidth}" Visibility="Collapsed"/>


    and on Loaded event of this control add:

            private void Grid_Loaded(object sender, RoutedEventArgs e)
            {
                ((Grid)sender).DataContext = this;
            }



    Thursday, May 19, 2011 2:22 PM

All replies

  • try:
     
    <UserControl ... Name="myPage">
    ...
     <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Vertical" >
                                <TextBlock TextWrapping="Wrap" x:Name="txtAnnotation"
                                            Width="{Binding ElementName=myPage, Path=TextLineWidth}"
                                          Text="{Binding Path=LayoutAnnotation.ANNOTATION.ANNOTATION1}" />
                                <StackPanel Orientation="Horizontal" Margin="10,0,0,0">
                                    <TextBlock Width="70"  Style="{StaticResource TBDateTimeStamp }"
                                            Text="{Binding Path=LayoutAnnotation.ANNOTATION.CREATED_ON}" />
                                    <Button Margin="5,0,0,0" VerticalAlignment="Top"
                                            Style="{StaticResource closeButtonStyle }" x:Name="btnRemove" Click="btnRemove_Click"  
                                        Visibility="{Binding Path=DeleteVisible}" />
                                </StackPanel>
                            </StackPanel>
                        </DataTemplate>
    </ListBox.ItemTemplate>

    Thursday, May 19, 2011 2:12 PM
  • You can bind it to some other control Width, for instance:


    <TextBlock TextWrapping="Wrap" x:Name="txtAnnotation" 
                                            Width="{Binding Width,ElementName=pattern}"
                                          Text="{Binding Path=LayoutAnnotation.ANNOTATION.ANNOTATION1}" />


    To your xaml add element with name "pattern" for instance:

    <Grid x:Name="pattern" Loaded="Grid_Loaded" Width="{Binding TextLineWidth}" Visibility="Collapsed"/>


    and on Loaded event of this control add:

            private void Grid_Loaded(object sender, RoutedEventArgs e)
            {
                ((Grid)sender).DataContext = this;
            }



    Thursday, May 19, 2011 2:22 PM