gpt4 book ai didi

c# - 根据滚动位置动态显示 ComboBox 的元素

转载 作者:太空宇宙 更新时间:2023-11-03 12:30:28 26 4
gpt4 key购买 nike

我正在尝试制作一个 ComboBox,根据滚动的位置动态显示其 ItemSource 中的元素,类似于加载更多内容的 Facebook 新闻提要当您到达滚动查看器的末尾时。

我考虑过显示 ItemSource 中的前 20 个元素并将其余元素折叠起来,这样当滚动条到达底部时它们就会变得可见,但我还没有成功。

这有可能实现吗?或者认为这可以做到太疯狂了?

此外,我正在为这个 ComboBox 使用自定义样式

<Style x:Key="CustomComboBox" TargetType="ComboBox">
<Setter Property="SnapsToDevicePixels" Value="true" />
<Setter Property="OverridesDefaultStyle" Value="true" />
<Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Visible" />
<Setter Property="ScrollViewer.CanContentScroll" Value="true" />
<Setter Property="IsEditable" Value="True"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="FontFamily" Value="Coves"/>
<Setter Property="FontWeight" Value="Normal"/>
<Setter Property="FontSize" Value="12"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ComboBox">
<Border x:Name="TopBorder"
CornerRadius="8"
BorderBrush="Grey"
BorderThickness="1"
Padding="10,0,1,0">
<Border.Background>
<LinearGradientBrush EndPoint="0.504,1.5" StartPoint="0.504,0.03">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="#e3e3e5" Offset="0.65"/>
</LinearGradientBrush>
</Border.Background>
<Grid>
<ToggleButton
Name="ToggleButton"
Template="{DynamicResource CustomComboBoxToggleButton}"
Grid.Column="2"
Focusable="false"
IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay,RelativeSource={RelativeSource TemplatedParent}}"
ClickMode="Press">
</ToggleButton>
<TextBlock Name="ContentSite" IsHitTestVisible="False"
Text="{Binding Source={StaticResource Proxy}, Path=Data.Name, UpdateSourceTrigger=PropertyChanged}"
Visibility="Visible" Foreground="#37465c"
Padding="3,3,23,3" VerticalAlignment="Center"
HorizontalAlignment="Left"/>
<Popup x:Name="Popup" Placement="Bottom" IsOpen="{TemplateBinding IsDropDownOpen}" AllowsTransparency="True"
Focusable="False" PopupAnimation="Fade">
<StackPanel Orientation="Vertical" Width="215">
<Grid Name="DropDown" SnapsToDevicePixels="True"
MaxHeight="{TemplateBinding MaxDropDownHeight}">
<Border x:Name="DropDownBorder" BorderThickness="1"
BorderBrush="#888">
<Border.Background>
<LinearGradientBrush EndPoint="0.504,1.5" StartPoint="0.504,0.03">
<GradientStop Color="White" Offset="0"/>
<GradientStop Color="#e3e3e5" Offset="0.65"/>
</LinearGradientBrush>
</Border.Background>
<ScrollViewer Margin="0" SnapsToDevicePixels="True">
<StackPanel IsItemsHost="True" KeyboardNavigation.DirectionalNavigation="Contained"/>
</ScrollViewer>
</Border>
</Grid>
</StackPanel>
</Popup>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Cursor" Value="Hand"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

非常欢迎任何想法、建议或意见。非常感谢!

最佳答案

执行此操作的最简单方法可能是子类化 ComboBox 并修改它以创建 PagingComboBox。下面是一个小例子。不完美但有效。

XAML 用法

 <PagingComboBox PagingSource="{Binding YourSource}" PageSize="15"/>

控制

public class PagingComboBox : ComboBox {

public PagingComboBox() {
this.ItemsSource = new ObservableCollection<object>();
this.Loaded += this.MyComboboxLoaded;
}

private int _currentItems = 0;
private void MyComboboxLoaded(object sender, RoutedEventArgs e) {
for (int i=0; i < this.PageSize; i++)
{
(this.ItemsSource as ObservableCollection<object>).Add(this.PagingSource[i]);
this._currentItems++;
}

var sv = this.Template.FindName("DropDownScrollViewer", this) as ScrollViewer;
sv.ScrollChanged += this.SvScrollChanged;

}

private void SvScrollChanged(object sender, ScrollChangedEventArgs e) {
var sv = sender as ScrollViewer;
var isAtEnd = sv.VerticalOffset == sv.ScrollableHeight;
var cLimmit = this._currentItems + this.PageSize;
if (isAtEnd && this._currentItems < this.PagingSource.Count) {
for (int i = this._currentItems; i < cLimmit; i++)
{
if (i > this.PagingSource.Count - 1) return;
(this.ItemsSource as ObservableCollection<object>).Add(this.PagingSource[i]);
this._currentItems++;
}
}
}

public int PageSize {
get {
return (int)GetValue(PageSizeProperty);
}
set {
SetValue(PageSizeProperty, value);
}
}

// Using a DependencyProperty as the backing store for PageSize. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PageSizeProperty =
DependencyProperty.Register("PageSize", typeof(int), typeof(PagingComboBox), new PropertyMetadata(20));



public IList PagingSource {
get {
return (IList)GetValue(PagingSourceProperty);
}
set {
SetValue(PagingSourceProperty, value);
}
}

// Using a DependencyProperty as the backing store for MyProperty. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PagingSourceProperty =
DependencyProperty.Register("PagingSource", typeof(IList), typeof(PagingComboBox), new PropertyMetadata(null));



}

注意

您可能需要为滚动查看器命名 (DropDownScrollViewer),因为您为它创建了自定义模板。一个缺点可能是组合框的原始 ItemsSource-Property 丢失。

希望这能为您提供有关如何实现此类功能的线索。

关于c# - 根据滚动位置动态显示 ComboBox 的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43037434/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com