【问题标题】:WPF window resizing and ActualWidth / ActualHeight valuesWPF 窗口调整大小和 ActualWidth / ActualHeight 值
【发布时间】:2013-07-16 14:55:08
【问题描述】:

在我的 WPF 项目中,我有一个 Canvas,我在其中做一些绘图。按照窗口的代码:

<Window x:Name="PropertyDefinition_Window" x:Class="PushOverStraus7.PropertyDefinitionWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="PropertyDefinitionWindow" Height="360" Width="730" Loaded="Window_Loaded">
<Grid>
    <TreeView SelectedItemChanged="Treeview_PropertyDefinition_SelectedItemChanged">
        <TreeViewItem IsExpanded="True">
            <TreeViewItem />
        </TreeViewItem>
        <TreeViewItem>
            <TreeViewItem"/>
        </TreeViewItem>
    </TreeView>
    <Canvas x:Name="Canvas_LongitudinalBarRectanglePage" HorizontalAlignment="Left" Height="251" Margin="255,10,0,0" VerticalAlignment="Top" Width="285" Background="Black">            
    </Canvas> 
    <Button x:Name="NewButton" Content="Nuovo" HorizontalAlignment="Left" Margin="22,277,0,0" VerticalAlignment="Top" Width="45" Click="NewButton_Click"/>
    <Button x:Name="DuplicateButton" Content="Duplica" HorizontalAlignment="Left" Margin="85,277,0,0" VerticalAlignment="Top" Width="45"/>

</Grid>

我想要这样,调整窗口大小,Canvas 大小会相应改变。然后,我需要调整 Canvas 的 ActualWidthActualHeight 的大小以使用它们来绘制算法。 如何自动调整 Canvas 的大小以及如何获取上述值? 感谢您的帮助

编辑: 我已按要求更改了问题

【问题讨论】:

  • 请同时显示您设置框架内容的代码。为什么你需要一个框架呢?您似乎没有使用框架导航。
  • Frame 展示起来不太有用,因为它不会影响 Canvas 的行为。正如您在我的编辑中所读到的那样,我从页面(以及因此从框架)中删除了画布,因此画布显然位于窗口内,具有定义的宽度和高度
  • 也许您将问题简化为您实际在做什么。删除那些不再相关的东西,并请添加相关的代码隐藏部分。

标签: wpf xaml resize window


【解决方案1】:

使用以下 XAML,当窗口大小发生变化时,画布将自动调整大小。

<Window ...>
    <Frame>
        <Frame.Content>
            <Page>
                <Canvas>
                    ...
                </Canvas>
            </Page>
        </Frame.Content>
    </Frame>
</Window>

【讨论】:

  • 原来Frame继承了Window的大小?还是两者兼而有之?
  • 我无法测试它,因为我要管理两种不同的 XAML,一种用于窗口,一种用于页面...我通过代码设置框架内容,因为它以编程方式更改,选择树视图中的项目。所以我不能在上面使用 XAML
  • 那你为什么不在你的问题中发布你的代码的相关部分呢?
  • 你说得对...对不起我不是很清楚...我把代码放在帖子里
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-27
  • 2011-02-13
  • 2013-06-24
  • 2013-07-12
  • 2019-07-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多