You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET 3.5下ItemsControl调试输出报System.Windows.Data Error:25求助

解决WPF中ItemTemplate不适用于CheckBox类型的调试错误

调试输出持续被以下错误信息刷屏,功能符合预期但错误干扰正常调试:

System.Windows.Data Error: 25 : ItemTemplate and ItemTemplateSelector are not valid for item Type='CheckBox'

对应的XAML代码:

<ScrollViewer CanContentScroll="True" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled">
            <ItemsControl ItemsSource="{Binding WrapPanel}">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <CheckBox  IsChecked="{Binding IsChecked}" ToolTip="{Binding ToolTip}" Margin="3,0,0,0">
                            <TextBlock Text="{Binding Content}" TextWrapping="Wrap" Width="250"></TextBlock>
                        </CheckBox>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Vertical" Height="735" Margin="0,10,0,0" ScrollViewer.VerticalScrollBarVisibility="Visible" ItemWidth="290" ItemHeight="20">

                        </WrapPanel>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </ScrollViewer>

补充说明:必须使用.NET 3.5,无法升级到更高版本,重命名绑定集合无效。


解决方案

错误根源是**.NET 3.5中的WrapPanel不支持直接设置ItemWidth和ItemHeight属性**,这两个属性属于ItemsControl而非WrapPanel。将这两个属性从WrapPanel移到ItemsControl上即可消除错误。

修改后的XAML代码:

<ScrollViewer CanContentScroll="True" HorizontalScrollBarVisibility="Visible" VerticalScrollBarVisibility="Disabled">
            <ItemsControl ItemsSource="{Binding WrapPanel}" ItemWidth="290" ItemHeight="20">
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <CheckBox  IsChecked="{Binding IsChecked}" ToolTip="{Binding ToolTip}" Margin="3,0,0,0">
                            <TextBlock Text="{Binding Content}" TextWrapping="Wrap" Width="250"></TextBlock>
                        </CheckBox>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <WrapPanel Orientation="Vertical" Height="735" Margin="0,10,0,0" ScrollViewer.VerticalScrollBarVisibility="Visible">

                        </WrapPanel>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </ScrollViewer>

原因说明

在.NET 3.5中,WrapPanel本身没有ItemWidth和ItemHeight属性,当你在WrapPanel上设置这两个属性时,WPF会尝试将其应用到每个子元素(即CheckBox),但CheckBox不具备这些属性,同时该操作与ItemsControl的ItemTemplate设置产生冲突,从而触发调试错误。将属性移到ItemsControl后,ItemsControl会正确地将尺寸应用到由ItemTemplate生成的容器(ContentPresenter)上,而非直接作用于CheckBox,错误即可消除。

内容的提问来源于stack exchange,提问作者Deagle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.23 10:54:18