简体   繁体   English

ListBox的DataTemplate中的数据绑定

[英]Data binding in DataTemplate of ListBox

I have a list box defined as below in xaml: 我在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). 我想将tb_Text的某些属性(例如text属性)绑定到列表框的datacontext的某些属性(在此示例中为IsSelected属性)。 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. 注意: IsSelected属性来自模板化父级,该模板化父级在模板中定义了此列表框。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM