.net 如何将数据触发器链接到ScrollViewer是否可以向上滚动?
我想在ScrollViewer的顶部和底部显示一个半不透明的箭头,如果它可以分别向上或向下滚动的话。我认为最好的选择是DataTrigger,但我也不确定我能用什么来绑定它。我试图避免将ScrollViewer子类化,但如果我必须这样做,我会这样做。有什么想法吗 我正在使用.NETFramework3.5(我希望我能升级!).net 如何将数据触发器链接到ScrollViewer是否可以向上滚动?,.net,wpf,scrollviewer,datatrigger,.net,Wpf,Scrollviewer,Datatrigger,我想在ScrollViewer的顶部和底部显示一个半不透明的箭头,如果它可以分别向上或向下滚动的话。我认为最好的选择是DataTrigger,但我也不确定我能用什么来绑定它。我试图避免将ScrollViewer子类化,但如果我必须这样做,我会这样做。有什么想法吗 我正在使用.NETFramework3.5(我希望我能升级!) 谢谢。:) 可能的解决方案之一。它使用两个转换器来计算是否可以滚动。该模板基于标准的ScrollViewer模板,但有两个额外的文本块来显示信息(“箭头”) Window1
谢谢。:) 可能的解决方案之一。它使用两个转换器来计算是否可以滚动。该模板基于标准的
ScrollViewer
模板,但有两个额外的文本块来显示信息(“箭头”)
Window1.xaml
Window1.xaml.cs
使用系统;
利用制度全球化;
使用System.Windows.Data;
命名空间WpfApplication1
{
公共部分类窗口1
{
公共常数双ε=0.001;
公共窗口1()
{
初始化组件();
}
}
公共类ScrollViewerTranscrollUpConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
返回Math.Abs((双精度)值)>Window1.Epsilon;
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
抛出新的NotSupportedException();
}
}
公共类ScrollViewerTranscrollDownConverter:IMultiValueConverter
{
公共对象转换(对象[]值,类型targetType,对象参数,CultureInfo区域性)
{
双垂直偏移=(双)值[0];
double extentHeight=(双)值[1];
双视口高度=(双)值[2];
double maxOffset=Math.Max(0.0,ExtentheRight-视口高度);
返回垂直偏移
不是我最后做的,但非常好!最后,我在代码中创建了属性,该属性返回了滚动条是否可以向上或向下滚动的布尔值。我命名了我的用户控件,并通过该控件访问它们。:)
<Window x:Class="WpfApplication1.Window1"
Title="Window1" Height="300" Width="300"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:WpfApplication1="clr-namespace:WpfApplication1">
<Window.Resources>
<ResourceDictionary>
<WpfApplication1:ScrollViewerCanScrollUpConverter x:Key="ScrollViewerCanScrollUpConverter" />
<WpfApplication1:ScrollViewerCanScrollDownConverter x:Key="ScrollViewerCanScrollDownConverter" />
</ResourceDictionary>
</Window.Resources>
<Grid>
<ScrollViewer Background="Transparent">
<ScrollViewer.Template>
<ControlTemplate TargetType="{x:Type ScrollViewer}">
<Grid x:Name="Grid" Background="{TemplateBinding Background}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Rectangle x:Name="Corner" Fill="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" Grid.Column="1" Grid.Row="1"/>
<ScrollContentPresenter Margin="{TemplateBinding Padding}" x:Name="PART_ScrollContentPresenter" Grid.Column="0" Grid.Row="0" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False"/>
<ScrollBar Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" Cursor="Arrow" x:Name="PART_VerticalScrollBar" ViewportSize="{TemplateBinding ViewportHeight}" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Value="{Binding Path=VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" Grid.Column="1" Grid.Row="0" AutomationProperties.AutomationId="VerticalScrollBar"/>
<ScrollBar Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" Cursor="Arrow" x:Name="PART_HorizontalScrollBar" Orientation="Horizontal" ViewportSize="{TemplateBinding ViewportWidth}" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Value="{Binding Path=HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" Grid.Column="0" Grid.Row="1" AutomationProperties.AutomationId="HorizontalScrollBar"/>
<TextBlock x:Name="PART_UpTextBlock" VerticalAlignment="Top" Text="Can scroll up" Visibility="Collapsed" />
<TextBlock x:Name="PART_DownTextBlock" VerticalAlignment="Bottom" Text="Can scroll down" Visibility="Collapsed" />
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Value="True" Binding="{Binding Path=VerticalOffset, RelativeSource={RelativeSource Self}, Converter={StaticResource ScrollViewerCanScrollUpConverter}}">
<Setter TargetName="PART_UpTextBlock" Property="Visibility" Value="Visible" />
</DataTrigger>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource ScrollViewerCanScrollDownConverter}">
<Binding Path="VerticalOffset" RelativeSource="{RelativeSource Self}" />
<Binding Path="ExtentHeight" RelativeSource="{RelativeSource Self}" />
<Binding Path="ViewportHeight" RelativeSource="{RelativeSource Self}" />
</MultiBinding>
</DataTrigger.Binding>
<Setter TargetName="PART_DownTextBlock" Property="Visibility" Value="Visible" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ScrollViewer.Template>
<Border Margin="10" Height="400" Background="Yellow">
<TextBlock Text="Content" />
</Border>
</ScrollViewer>
</Grid>
</Window>
using System;
using System.Globalization;
using System.Windows.Data;
namespace WpfApplication1
{
public partial class Window1
{
public const double Epsilon = 0.001;
public Window1()
{
InitializeComponent();
}
}
public class ScrollViewerCanScrollUpConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return Math.Abs((double)value) > Window1.Epsilon;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
public class ScrollViewerCanScrollDownConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
double verticalOffset = (double)values[0];
double extentHeight = (double)values[1];
double viewportHeight = (double)values[2];
double maxOffset = Math.Max(0.0, extentHeight - viewportHeight);
return verticalOffset < maxOffset - Window1.Epsilon;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
}