Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF Xaml-匹配宽度_C#_Wpf_Xaml_Alignment - Fatal编程技术网

C# WPF Xaml-匹配宽度

C# WPF Xaml-匹配宽度,c#,wpf,xaml,alignment,C#,Wpf,Xaml,Alignment,我有下面的xaml,在上面可以看到一个GroupBox,在左边和右边可以看到其他的GroupBox。 是否有办法将GroupBox设置在顶部,以便在调整窗口大小时,其左边缘和右边缘分别与左侧GroupBox的左边缘和右侧GroupBox的右边缘对齐 编辑 我在选项卡控件中固定了GroupBox的宽度,因为我在那里实现了wpf缩放:我现在更新了xaml,当然缩放也在后面的代码中实现 <?xml version="1.0" encoding="utf-8"?> <Window

我有下面的xaml,在上面可以看到一个GroupBox,在左边和右边可以看到其他的GroupBox。 是否有办法将GroupBox设置在顶部,以便在调整窗口大小时,其左边缘和右边缘分别与左侧GroupBox的左边缘和右侧GroupBox的右边缘对齐

编辑

我在选项卡控件中固定了GroupBox的宽度,因为我在那里实现了wpf缩放:我现在更新了xaml,当然缩放也在后面的代码中实现

<?xml version="1.0" encoding="utf-8"?>
<Window
    x:Class="MatchWidth.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MatchWidth"
    Height="1000"
    Width="1600">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition
                Height="100" />
            <RowDefinition
                Height="*" />
        </Grid.RowDefinitions>
        <GroupBox
            Header="Top Box"
            Grid.Row="0"
            HorizontalAlignment="Stretch"
            VerticalAlignment="Stretch"
            Margin="25,5,35,25" />
        <Grid
            Grid.Row="1">
            <Grid.LayoutTransform>
                <ScaleTransform
                    CenterX="0" CenterY="0"
                    ScaleX="{Binding ElementName=uiScaleSliderL,Path=Value}"
                    ScaleY="{Binding ElementName=uiScaleSliderL,Path=Value}"/>
            </Grid.LayoutTransform>
            <Grid.ColumnDefinitions>
                <ColumnDefinition
                    Width="*" />
                <ColumnDefinition
                    Width="5" />
                <ColumnDefinition
                    Width="*" />
            </Grid.ColumnDefinitions>
            <TabControl
                Name="LeftTabCtr"
                Grid.Column="0">
                <TabItem Header="LeftTabCtr">
                    <ScrollViewer
                        HorizontalScrollBarVisibility="Auto">
                        <Grid
                            Height="800">
                            <Slider
                                x:Name="uiScaleSliderL"
                                ToolTip="Determines the UI scale factor."
                                Value="1" Minimum="0.1" Maximum="4" Width="200" Height="10"
                                HorizontalAlignment="Center" VerticalAlignment="Top" />
                            <GroupBox
                                Header="Left Box 1"
                                Grid.Column="0"
                                Grid.Row="0"
                                HorizontalAlignment="Stretch"
                                VerticalAlignment="Top"
                                Margin="25,20,25,25"
                                Width="720"
                                Height="180"/>
                            <GroupBox
                                Header="Left Box 2"
                                Grid.Column="0"
                                Grid.Row="0"
                                HorizontalAlignment="Stretch"
                                VerticalAlignment="Stretch"
                                Width="720"
                                Margin="25,220,25,10" />
                        </Grid>
                    </ScrollViewer>
                </TabItem>
            </TabControl>
            <GridSplitter
                Grid.Column="1"
                Width="5"
                HorizontalAlignment="Stretch" />
            <TabControl
                Name="RightTabCtr"
                Grid.Column="2">
                <TabItem Header="RightTabCtr">
                    <ScrollViewer
                        HorizontalScrollBarVisibility="Auto">
                        <Grid
                            Height="800">            <Slider
                                x:Name="uiScaleSliderR"
                                ToolTip="Determines the UI scale factor."
                                Value="1" Minimum="0.1" Maximum="4" Width="200" Height="10"
                                HorizontalAlignment="Center" VerticalAlignment="Top" />
                            <GroupBox
                                Header="Right Box 1"
                                Grid.Column="0"
                                Grid.Row="0"
                                HorizontalAlignment="Stretch"
                                VerticalAlignment="Top"
                                Margin="25,20,25,25"
                                Width="720"
                                Height="180"/>
                            <GroupBox
                                Header="Right Box 2"
                                Grid.Column="0"
                                Grid.Row="0"
                                HorizontalAlignment="Stretch"
                                VerticalAlignment="Stretch"
                                Width="720"
                                Margin="25,220,25,10" />
                        </Grid>
                    </ScrollViewer>
                </TabItem>
            </TabControl>
        </Grid>
    </Grid>
</Window>

您必须指定正确的边距并删除固定宽度的GroupBox,在您的情况下,使用minWidth可能更灵活,或者完全删除它

您仍然可以在GroupBox中使用缩放内容,只需将所需的大小设置为控件,但不设置为Groupbox本身,或者可以将其放置在具有自定义宽度的网格中,但让Groupbox为非恒定宽度,但不获取任何内容-什么控件应用此缩放机制?你能用“缩放到viewBox”来包装控件吗?
public partial class Window1 : Window
{
    public Window1()
    {
        InitializeComponent();
        uiScaleSliderL.MouseDoubleClick +=
            new MouseButtonEventHandler(RestoreScalingFactorL);
    }

    protected override void OnPreviewMouseWheel(MouseWheelEventArgs args)
    {
        base.OnPreviewMouseWheel(args);
        if (Keyboard.IsKeyDown(Key.LeftCtrl) ||
            Keyboard.IsKeyDown(Key.RightCtrl))
        {
            uiScaleSliderL.Value += (args.Delta > 0) ? 0.1 : -0.1;
        }
    }

    protected override void OnPreviewMouseDown(MouseButtonEventArgs args)
    {
        base.OnPreviewMouseDown(args);
        if (Keyboard.IsKeyDown(Key.LeftCtrl) ||
            Keyboard.IsKeyDown(Key.RightCtrl))
        {
            if (args.MiddleButton == MouseButtonState.Pressed)
            {
                RestoreScalingFactorL(uiScaleSliderL, args);
            }
        }
    }
    void RestoreScalingFactorL(object sender, MouseButtonEventArgs args)
    {
        ((Slider)sender).Value = 1.0;
    }
}