简体   繁体   中英

Data binding in DataTemplate of ListBox

I have a list box defined as below in xaml:

<ListBox>
   <ListBox.ItemContainerStyle>
      <Style TargetType="ListBoxItem">
         <Setter Property="Template">
            <Setter.Value>
               <ControlTemplate>
                  <Grid>
                     <Ellipse Visibility="{Binding IsSelected, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource BooleanToVisibilityConverter}}" />
                         <ContentPresenter/>
                  </Grid>
               </ControlTemplate>
             </Setter.Value>
          </Setter>
      </Style>
   </ListBox.ItemContainerStyle>
   <ListBox.ItemTemplate>
      <DataTemplate>
        <StackPanel>
          <TextBlock x:Name="tb_Text" Text="{Binding IsSelected}"> </TextBlock>
         </StackPanel>
      </DataTemplate>
   </ListBox.ItemTemplate>

I want to bind some property of tb_Text (say text property) to some property of the datacontext of the listbox (IsSelected property in this example). Is there a way to achieve that?

Note: IsSelected property is coming from the templated parent which has this listbox defined in it's Template.

使用ElementName并绑定到EllipseVisibility属性可以解决问题。

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