ListView的ItemContainerStyle模板

ListView的ItemContainerStyle模板

本文介绍了ListView的ItemContainerStyle模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的样式用作包含CheckBox和一个TextBlock我的ListView的项模板:

I created style to be used as my ListView's item template that contains a CheckBox and a TextBlock:

<Style x:Key="CheckBoxListStyle" TargetType="{x:Type ListView}">
            <Setter Property="SelectionMode" Value="Multiple"></Setter>
            <Setter Property="ItemContainerStyle">
                <Setter.Value>
                    <Style TargetType="{x:Type ListViewItem}" >
                        <Setter Property="Template">
...

在模板中的复选框被绑定到列表视图项的IsSelected属性:

The checkbox in the template is bound to the IsSelected property of the list view item:

<CheckBox x:Name="itemCheckBox" Width="13" Height="13" Focusable="False" IsChecked="{Binding Path=IsSelected, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}">

与文本块的文本被绑定到列表视图项目的来源的Value属性:

And the textblock's text is bound to the Value property of the source of the list view item:

<TextBlock x:Name="textBlock" Padding="5,0,0,0" HorizontalAlignment="Stretch">
  <ContentPresenter Content="{Binding Path=Value}"/>
</TextBlock>

每个在我的列表中的项目是包含两个属性的对象,器isChecked 。我所要做的是我的对象的财产器isChecked绑定到我的列表视图项的IsSelected属性。但是,我不知道如何从ListViewItem的模板内访问我的对象的财产器isChecked

Each of the items in my list is an object that contains two properties, Value and IsChecked. What I am trying to do is bind the IsChecked property of my object to the IsSelected property of my list view item. However, I do not know how to access the IsChecked property of my object from within the ListViewItem template.

我没有任何麻烦结合文本块到我对象的Value属性的内容,但我会在哪里甚至把绑定定义,如果我想我的对象的财产器isChecked要绑定到IsSelected属性列表视图项目?

I didn't have any trouble binding the content of the text block to the Value property of my object but where would I even put the binding definition if I want the IsChecked property of my object to be bound to the IsSelected property of the list view item?

推荐答案

在DataContext每个ListViewItem的,应当由父ListView控件创建,所以你应该能够使用设置项目数据:

The DataContext for each ListViewItem should be set to the item data when it is created by the parent ListView so you should be able to use:

<Style TargetType="{x:Type ListViewItem}" >
    <Setter Property="IsSelected" Value="{Binding IsChecked}">
    ...
</Style>

这篇关于ListView的ItemContainerStyle模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 00:52