简体   繁体   English

XAML-如何动态设置ScrollViewer的大小?

[英]XAML- how to set the size of a ScrollViewer dynamically?

I am displaying a <ScrollViewer> in my application window using XAML. 我正在使用XAML在我的应用程序窗口中显示<ScrollViewer> At the moment, I am specifying the size of the <ScrollViewer> statically (ie giving it a set value, which cannot be changed while the application is running): 目前,我正在静态地指定<ScrollViewer>的大小(即给它一个设置值,该值在应用程序运行时无法更改):

<Window>
    <Grid>
        <TabControl>
            <TabItem>
                <StackPanel>
                    <Grid>
                        <ScrollViewer x:Name="scroller" HorizontalAlignment="Right" VerticalScrollBarVisibility="Auto" Margin="39,10,0,0" VerticalAlignment="Top" Height="244" Width="450">
                            <TextBox x:Name="infoPane" TextWrapping="Wrap" Text="nothing" IsReadOnly="True" />
                        </ScrollViewer>
                    </Grid>
                </StackPanel>
            </TabItem>
        </TabControl>
    </Grid>
</Window>

What I want to do, is dynamically set the width of the <ScrollViewer> to half the width of the application window, so that it will always cover half of the displayed content. 我想做的是将<ScrollViewer>的宽度动态设置为应用程序窗口的一半宽度,以便它始终覆盖显示内容的一半。

The user can re-size the application window by dragging one of the corners or edges of the window, so the area of the display that the <ScrollViewer> takes up should automatically always be half of the width of the application window, whatever that is. 用户可以通过拖动窗口的拐角或边缘之一来调整应用程序窗口的大小,因此<ScrollViewer>占用的显示区域应始终自动为应用程序窗口宽度的一半,无论。

Is there a way that I can dynamically set the value of the <ScrollViewer> Width property? 有没有一种方法可以动态设置<ScrollViewer> Width属性的值?

To get the width and height of current window call Window.Current.Bounds.Width / .Height . 要获取当前窗口的宽度和高度,请调用Window.Current.Bounds.Width / .Height Just divide by two to get what you want. 只需除以二即可得到您想要的。 Setting the value dynamically is also easy: scroller.Width = value . 动态设置值也很容易: scroller.Width = value You could consider creating a Grid with 2 Columns both with Width="*" and place the ScrollViewer in one of them to get the exact same effect you want. 您可以考虑创建一个带有2列都为Width="*"的网格,并将ScrollViewer放在其中之一中以获得与您想要的效果完全相同的效果。

EDIT 编辑

Here's what I meant in the second part of my answer: 这就是我在答案的第二部分中表示的意思:

<Window>
<Grid>
    <TabControl>
        <TabItem>
            <StackPanel>
                <Grid>
                     <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />

                     </Grid.ColumnDefinitions>
                    <ScrollViewer x:Name="scroller" Grid.Column="0" HorizontalAlignment="Stretch" VerticalScrollBarVisibility="Auto" Margin="39,10,0,0" VerticalAlignment="Top" Height="244" Width="{Binding ActualWidth,RelativeSource={RelativeSource 
                           Mode=FindAncestor, AncestorType=Grid}}">
                        <TextBox x:Name="infoPane" TextWrapping="Wrap" Text="nothing" IsReadOnly="True" />
                    </ScrollViewer>
                </Grid>
            </StackPanel>
        </TabItem>
    </TabControl>
</Grid>
</Window>

Oh my bad, I misunderstood : Can you give your whole XAML ? 噢,我的天哪,我误解了:您能提供您的整个XAML吗?

Dynamic way is : width="x*" Where x is a number. 动态方式是: width="x*"其中x是数字。 But if you give the whole XAML I could make an example. 但是,如果您给出整个XAML,我可以举个例子。

Edit : 编辑:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="1*" />
        <ColumnDefinition Width="1*" />
    </Grid.ColumnDefinitions>

    <ScrollViewer x:Name="scroller" VerticalScrollBarVisibility="Auto">
        <TextBox x:Name="infoPane" TextWrapping="Wrap" Text="nothing" IsReadOnly="True" />
    </ScrollViewer>

</Grid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM