如何在CalendarDayButton模板中包含的ScrollViewer中启用滚动手柄的拖动

时间:2018-12-27 20:13:10

标签: wpf wpf-controls

在下面的示例中,CalendarDayButton控件模板内ListBox的滚动条将不会通过拖动滚动手柄来滚动。但是,单击滚动条效果很好。我最好的猜测是Calendar或CalendarDayButton中的某件事正在超出我的理解范围来处理鼠标事件。

是否可以恢复ScrollViewer的拖动-n-滚动行为?

<Window 
x:Class="CustomCalendar.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
Title="MainWindow" 
Width="500"
Height="400"
>
<Window.Resources>
    <Style x:Key="CalendarDayButtonStyle1" TargetType="{x:Type CalendarDayButton}">
        <Setter Property="MinWidth" Value="120"/>
        <Setter Property="MinHeight" Value="120"/>
        <Setter Property="FontSize" Value="12"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type CalendarDayButton}">
                    <Grid>
                        <Grid MaxHeight="220">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="30"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <ContentPresenter HorizontalAlignment="Left" x:Name="NormalText" TextElement.FontSize="14" TextElement.Foreground="#FF333333" Margin="1,0,1,0" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            <ListBox Grid.Row="1" Grid.ColumnSpan="9" Margin="0,0,0,0">
                                <ListBoxItem Content="Coffie"></ListBoxItem>
                                <ListBoxItem Content="Tea"></ListBoxItem>
                                <ListBoxItem Content="Orange Juice"></ListBoxItem>
                                <ListBoxItem Content="Milk"></ListBoxItem>
                                <ListBoxItem Content="Iced Tea"></ListBoxItem>
                                <ListBoxItem Content="Mango Shake"></ListBoxItem>
                                <ListBoxItem Content="Coffie"></ListBoxItem>
                                <ListBoxItem Content="Tea"></ListBoxItem>
                                <ListBoxItem Content="Orange Juice"></ListBoxItem>
                                <ListBoxItem Content="Milk"></ListBoxItem>
                                <ListBoxItem Content="Iced Tea"></ListBoxItem>
                                <ListBoxItem Content="Mango Shake"></ListBoxItem>
                                <ListBoxItem Content="Coffie"></ListBoxItem>
                                <ListBoxItem Content="Tea"></ListBoxItem>
                                <ListBoxItem Content="Orange Juice"></ListBoxItem>
                                <ListBoxItem Content="Milk"></ListBoxItem>
                                <ListBoxItem Content="Iced Tea"></ListBoxItem>
                                <ListBoxItem Content="Mango Shake"></ListBoxItem>
                            </ListBox>
                        </Grid>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <Calendar CalendarDayButtonStyle="{DynamicResource CalendarDayButtonStyle1}"/>
</Grid>

编辑:

看起来CalendarItem可能是这里的罪魁祸首。添加CalendarDayButton时,它会设置一些事件处理程序,尤其是MouseLeftButtonDownEvent。在该处理程序中,它正在捕获鼠标,这似乎使捕获脱离了Scroll手柄。寻找如何克服这一问题,这是我下一步的行动。

0 个答案:

没有答案