【问题标题】:CheckBox state of a CellTemplate in ListViewListView 中 CellTemplate 的 CheckBox 状态
【发布时间】:2011-04-20 08:37:25
【问题描述】:

我正在尝试使用样式来防止重复代码,方法是将它们放入资源字典中。

我的问题是,当我们在 ListView 中有一个 GridViewColumn 时,其中一列有一个 DataTemplate,而在那个 DataTemplate 中我们有一个只有 CheckBox 的 CellTemplate,当 DataTemplete 处于资源字典?

我的 XAML 中有这个:

<ListView Name="listView">
    <ListView.View>
        <GridView AllowsColumnReorder="False">
            <GridViewColumn DisplayMemberPath="{Binding [1]}"/>
            <GridViewColumn DisplayMemberPath="{Binding [2]}"/>
            <GridViewColumn DisplayMemberPath="{Binding [4]}"/>
            <GridViewColumn DisplayMemberPath="{Binding [5]}"/>
            <GridViewColumn DisplayMemberPath="{Binding [6]}"/>
            <GridViewColumn DisplayMemberPath="{Binding [7]}"/>
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <CheckBox IsThreeState="False" IsChecked="{Binding [8]}" Unchecked="CheckBox_Changed" Checked="CheckBox_Changed"/>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>

我正试图在资源字典中做这样的事情:

<DataTemplate x:Key="ListViewCheckboxCell">
    <StackPanel>
        <CheckBox IsThreeState="False" IsChecked="Make reference"/>
    </StackPanel>
</DataTemplate>

并且该列中的值始终是布尔值。

提前致谢!

【问题讨论】:

    标签: wpf xaml styles resourcedictionary


    【解决方案1】:

    你所做的似乎是正确的。你现在必须写

    <GridViewColumn CellTemplate="{StaticResource ListViewCheckboxCell}" />
    

    将考虑模板,您可以在资源字典中保留与原始模板完全相同的模板:绑定是动态解析的,因此当读取 XAML 时,绑定将自动设置为相关对象,按照你的指示

    【讨论】:

    • 但是我没有启用 AutoGenerateColumns 选项,我将源中的列绑定到 ListView 中的列中。所以我这样做&lt;GridViewColumn DisplayMemeberPath="{Binding [#]}" CellTemplate="{StaticResource ListViewCheckboxCell}" 但这样做会出现布尔值而不是复选框。这就是我遇到问题的地方......
    • 能否请您发布您的 ListView 的整个 XAML 代码?我想我对您的问题有所了解,几天前我遇到了几乎相同的问题。如果可能,请编辑您的原始帖子,为您的 ListView 和列表绑定到的原始集合添加整个代码(ItemsSource)
    • 我已经添加了我的 Listview 代码。 ItemsSource 是我将从数据库中获取的表。但是我将绑定的表中的列并不总是相同的。我将在其他窗口中使用此复选框模板,并且我将绑定的列具有不同的索引。
    • 那么,如果我理解正确,复选框将绑定到数据库中每列的不同列?我需要知道你从数据库中得到了什么,以及列是什么。如果我认为是您所拥有的,我鼓励您从数据库中的值创建一个Dictionary,将键(您的列详细信息)链接到它的值(复选框),因此您只需绑定到键和值没有任何更多与神奇数字的硬编码绑定
    • Yes 并不总是相同的列,因为它将是来自同一数据库的不同表。从 1 到 7 的列是文本,其中一个是日期,但最后一列是布尔值。行示例(Nvarchar、Nvarchar、Nvarchar、Nvarchar、Date、Nvarchar(但为布尔值))。当您说来自 DB 中的值的字典时,您的意思是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-02
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-13
    相关资源
    最近更新 更多