【发布时间】:2016-09-14 20:43:55
【问题描述】:
我有以下 XAML 标记,用于显示我的 C# 应用程序的 GUI:
<Grid x:Name="templateRoot" ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
<Grid.ColumnDefinitions>
<ColumnDefinition x:Name="ColumnDefinition0"/>
<ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition x:Name="RowDefinition0" Height="Auto"/>
<RowDefinition x:Name="RowDefinition2" Height="0.05*"/>
<RowDefinition x:Name="RowDefinition1" Height="*"/>
</Grid.RowDefinitions>
<TabPanel x:Name="headerPanel" Background="White" Grid.Column="0" IsItemsHost="true" Margin="2,2,2,0" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1" VerticalAlignment="Top"/>
<StackPanel Grid.Row="1" Orientation="Horizontal" FlowDirection="RightToLeft">
</StackPanel>
<StackPanel Grid.Row="1" Orientation="Horizontal" FlowDirection="LeftToRight" Height="30" VerticalAlignment="Top">
<Button Style="{DynamicResource NoChromeButton}" Click="backBtn_Click" Margin="0,0,0,0" HorizontalAlignment="Left" >
<Image Source="C:\...\arrow_left.png" Height="30" Width="40" />
</Button>
<Button Style="{DynamicResource NoChromeButton}" Click="RefreshBtn_Click" Margin="0,0,0,0" HorizontalAlignment="Left" >
<Image Source="C:\...\arrow_loop3.png" Height="30" Width="30" />
</Button>
<Button Style="{DynamicResource NoChromeButton}" Click="referThis" Margin="0,0,0,0" HorizontalAlignment="Left" HorizontalContentAlignment="Left" Height="30" Width="80">
<TextBlock>Refer Patient</TextBlock>
</Button>
</StackPanel>
<Border x:Name="contentPanel" BorderBrush="Green" BorderThickness="5" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="2" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
<ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
</Grid>
上述标记嵌套在文件顶部的以下结构中:
<Window x:Class.... >
<Window.Resources>
<Style ...>
...
<Setter Property="Template">
<Setter.value>
<ControlTemplate TargetType="{x:Type TabControl}">
<!--(Above markup goes here) -->
在文件的后面,我有以下标记:
</ControlTemplate>
</Setter.value>
</Setter>
</Style>
...
</Window.Resources>
<Grid Name = "grid">
<Image x:Name="Connected" Source="...\abc.png" Height="50" Width="50" Margin="750, 0, -5, 640"></Image>
<!-- A few more image tags here -->
<TabControl ... >
<!-- XML for each of the tab items & their content here -->
</TabControl>
</Grid>
</Window>
此 XAML 显示以下 GUI:
从图片中可以看出,页面的“主要内容”与<StackPanel> 重叠,我在其中添加了导航按钮和我的应用程序的其他功能(返回、刷新等)。
我可以通过拖动其中一个角来调整窗口大小,并调整它以使按钮和其他功能都正确显示:
但是,也有可能将窗口调整得过大(即太大),这会导致在这些按钮和事物之间显示过多的空白,也会导致最右侧的图像被移动到与我原计划不同的位置:
我的应用程序窗口最右侧的图像是我复制到问题中的第三段代码中的 XAML 标记显示的图像。
所以,我的问题是 - 有没有一种方法可以“修复”<StackPanel>、<Grid>、<Style> 或 <TabPanel> 的标题的大小,我在其中显示导航按钮和其他图像,以便当用户拖动窗口的一个角时它们不会随窗口的其余部分一起调整大小?
我尝试将<TabControl> 的Height 属性设置为特定值(例如50),但这导致<TabControl> 的全部内容缩小到50...即所有内容然后窗口显示在窗口上只有 50 像素高的区域中......
基本上,无论用户如何与应用程序交互,我都希望此处显示的内容保持不变,并且只有在此下方显示的内容在用户与应用程序交互时动态更新。有人有什么建议吗?
【问题讨论】:
-
尝试将第 1 行的高度(现在的高度为 0.05*)设置为自动。我认为这可能会解决重叠问题
-
是的,这解决了重叠问题 - 干杯!
-
我将其添加为答案,以便您接受它:)
-
关于如何解决调整窗口大小问题的任何建议?
标签: c# wpf xaml layout presentation