wpf – 永久启用的滚动条可能吗?

我试图找到一种方法来永久启用ScrollBars / ScrollViewers,我遇到了问题.我正在开发一个数据输入应用程序,其中某些用户可能只具有“读取”权限.这需要禁用所有控件,以便它们无法进行任何更改.

禁用控件时,用户无法查看可滚动控件中的所有信息(Infragistics XamDataGrid,DevExpress GridControl,带有Wrap的TextBox,ListBox等).我希望我能“智取”微软,并在设置为false时实现Trigger将IsEnabled设置为true,但当然,它不起作用(出于各种原因,我敢肯定).

我现在一直专注于文本框,试图禁用内容,但保持ScrollViewer启用,没有运气.

我希望可能有一个解决方案,I.E.覆盖ScrollViewer的IsEnabled依赖项属性以始终返回true,或者提供某种不允许ScrollViewer被禁用的Style.

我知道TextBox有一个IsReadOnly属性,但这对我解决此问题所需的许多其他控件没有用.

有什么方法可以用一种相当简单的方式来解决这个问题(简单的方法就是我可以制作一个适用于所有地方的样式,而无需更改200个代码文件).

谢谢

编辑:这是我在示例项目中使用的代码,试图解决此问题.

<Window x:Class="WPFScrollViewerStyles.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Microsoft_Windows_Themes:ListBoxChrome x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" SnapsToDevicePixels="true">
                        <ScrollViewer x:Name="PART_ContentHost" HorizontalScrollBarVisibility="Visible" 
                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" IsEnabled="True" />
                    </Microsoft_Windows_Themes:ListBoxChrome>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="IsEnabled" Value="True" TargetName="PART_ContentHost" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid IsEnabled="False">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="100" />
        <RowDefinition Height="100" />
        <RowDefinition Height="100" />
    </Grid.RowDefinitions>

    <TextBox x:Name="txtScroller" Width="100" Height="100" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" TextWrapping="Wrap" ScrollViewer.CanContentScroll="True">
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras faucibus hendrerit lorem, auctor vehicula sem molestie nec. Quisque non massa quam. Nullam tempor blandit tempor. Integer in molestie ipsum. Donec augue mi, auctor vitae scelerisque a, vehicula ac ipsum. Etiam posuere vulputate augue quis convallis. Nullam aliquet purus nec lacus fermentum hendrerit egestas purus tincidunt. Suspendisse quis lacinia libero. Quisque facilisis turpis at augue dignissim aliquam ultrices sem porta. Etiam sagittis arcu id nibh ultrices dictum. Nulla non lectus luctus est malesuada luctus. Praesent sed lobortis nisi. Morbi et porttitor massa. Sed pellentesque, nisl eu imperdiet varius, ligula augue cursus nisl, eu egestas metus velit non elit. Phasellus elementum hendrerit risus, eu tincidunt ante gravida vel.
    </TextBox>

    <ListBox x:Name="lstScroller" Width="100" Height="100" Grid.Column="1">
        <ListBox.Items>
            <ListBoxItem Content="Item 1" />
            <ListBoxItem Content="Item 2" />
            <ListBoxItem Content="Item 3" />
            <ListBoxItem Content="Item 4" />
            <ListBoxItem Content="Item 5" />
            <ListBoxItem Content="Item 6" />
            <ListBoxItem Content="Item 7" />
            <ListBoxItem Content="Item 8" />
            <ListBoxItem Content="Item 9" />
            <ListBoxItem Content="Item 10" />
            <ListBoxItem Content="Item 11" />
            <ListBoxItem Content="Item 12" />
            <ListBoxItem Content="Item 13" />
            <ListBoxItem Content="Item 14" />
            <ListBoxItem Content="Item 15" />
        </ListBox.Items>
    </ListBox>
</Grid>
感谢Cedric Dussud在微软,我找到了一个解决方案.

供参考:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/3bf5e186-845c-4b63-acd9-17d294a30f77

我不是子类化ScrollViewer控件,而是覆盖应用程序的Application_Startup事件中的元数据,如下所示:

ScrollViewer.IsEnabledProperty.OverrideMetadata(typeof(ScrollViewer), new UIPropertyMetadata(true, new PropertyChangedCallback(ScrollViewer_IsEnabledPropertyChanged), new CoerceValueCallback(ScrollViewerForceEnabled)));

然后我处理CoerceValueCallback以始终返回true,如下所示:

private static void ScrollViewer_IsEnabledPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs args) { }
    private static object ScrollViewerForceEnabled(DependencyObject source, object value)
    {
        return true;
    }

根据Cedric的说法,这可能会产生无法预料的后果,但截至目前(以及我所有强大的测试应用程序都有一个禁用网格和两个可滚动控件)我还没找到.希望这个解决方案可以帮助其他人,没有问题.

相关文章
相关标签/搜索