WPF Combobox 控件遇到的一些问题
-
当设置Combobox可输入时,如果需要输入下拉选项中不存在的值,并可以正确更新到ViewModel中,可以使用Text属性绑定数据。使用SelectValue绑定时,只有输入的数据在下拉列表中,才会更新到ViewModel。
-
Combox数据源为IColleciontView时,如果设置IsSychronizedWithCurrentItem=false,则IColleciontView.CurrentItem不会与控件当前选项同步,且前端选项改变不会触发IColleciontView.CurrentItemChanged事件。
-
ComboBox加载大量数据时,可设置ItemsPanelTemplate,用VirtualizingStackPanel开启虚拟化。
<ComboBox>
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
但需要注意,在自定义控件的Template时,Popup下必须要有x:name为ItemsPresenter的元素,否则虚拟化不起作用。
<ControlTemplate x:Key="ComboBoxTemplate" TargetType="ComboBox">
<!--......省略其他代码-->
<Popup x:Name="PART_Popup">
<Border x:Name="dropDownBorder" MaxHeight="{TemplateBinding MaxDropDownHeight}" BorderBrush="{DynamicResource BorderBrush}" BorderThickness="1" Background="{DynamicResource RegionBrush}">
<ScrollViewer Margin="0,4" x:Name="DropDownScrollViewer">
<!--......省略其他代码-->
<!--x:Name="ItemsPresenter" 必须设置,否则虚拟化失效-->
<ItemsPresenter x:Name="ItemsPresenter" KeyboardNavigation.DirectionalNavigation="Contained" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
<!--......省略其他代码-->
</ScrollViewer>
</Border>
</Popup>
<!--......省略其他代码-->
</ControlTemplate>
个人开发过程中遇到的问题,如果有误,请指正,谢谢。

浙公网安备 33010602011771号