【问题标题】:Dynamic Viewbox not Stretching动态视图框不拉伸
【发布时间】:2016-10-25 22:46:47
【问题描述】:

在我的输入框earlier example 中,我有一个窗口变量。我创建了一些控件(文本框、标签、按钮)。这些控件的父级是画布。画布的父级是ViewBox(因为ViewBox 只能包含一个子级),ViewBox 的父级是窗口。 所以层次结构就像Window->Viewbox->Canvas-> Controls。所有这些控件的创建和父级都是动态完成的。

        winInputDialog = new Window();
        lblPrompt = new Label();
        btnOK = new Button();
        btnCancel = new Button();
        txtInput = new TextBox();
        cvContainer = new Canvas();
        VB = new Viewbox();

        // 
        // lblPrompt
        // 
        lblPrompt.Background = new SolidColorBrush(SystemColors.ControlColor);
        lblPrompt.FontFamily = new FontFamily("Microsoft Sans Serif");
        lblPrompt.FontSize = 12;
        lblPrompt.Background = new SolidColorBrush(Colors.Transparent);
        lblPrompt.FontStyle = FontStyles.Normal;
        lblPrompt.Margin = new Thickness(8, 9, 0, 0);
        lblPrompt.Name = "lblPrompt";
        lblPrompt.Width = 302;
        lblPrompt.Height = 82;
        lblPrompt.TabIndex = 3;
        // 
        // btnOK
        // 
        btnOK.Margin = new Thickness(322, 8, 0, 0);
        btnOK.Name = "btnOK";
        btnOK.Width = 64;
        btnOK.Height = 24;
        btnOK.TabIndex = 1;
        btnOK.Content = "OK";
        btnOK.Click += new RoutedEventHandler(btnOK_Click);
        // 
        // btnCancel
        //          
        btnCancel.Margin = new Thickness(322, 40, 0, 0);
        btnCancel.Name = "btnCancel";
        btnCancel.Width = 64;
        btnCancel.Height = 24;
        btnCancel.TabIndex = 2;
        btnCancel.Content = "Cancel";
        btnCancel.Click += new RoutedEventHandler(btnCancel_Click);
        // 
        // txtInput
        // 
        txtInput.Margin = new Thickness(8, 70, 0, 0);
        txtInput.Name = "txtInput";
        txtInput.Width = 379;
        txtInput.Height = 25;
        txtInput.TabIndex = 0;
        //
        //Canvas
        //

        double width = System.Windows.SystemParameters.PrimaryScreenWidth / 3, height = System.Windows.SystemParameters.PrimaryScreenHeight / 4;

        cvContainer.Height = height;
        cvContainer.Width = width;

        cvContainer.Children.Add(txtInput);
        cvContainer.Children.Add(btnCancel);
        cvContainer.Children.Add(btnOK);
        cvContainer.Children.Add(lblPrompt);
        cvContainer.ClipToBounds = true;
        //
        //ViewBox
        //            
        VB.Stretch = Stretch.Fill;
        VB.Child = cvContainer;
        // 
        // InputBoxDialog
        //
        winInputDialog.Width = width;
        winInputDialog.Height = height;            
        winInputDialog.Content = VB;
        winInputDialog.Icon = new System.Windows.Media.Imaging.BitmapImage(new System.Uri(System.IO.Directory.GetCurrentDirectory() + @"\drop-box-icon.png"));
        winInputDialog.WindowStartupLocation = WindowStartupLocation.CenterScreen;
        //winInputDialog.WindowStyle = WindowStyle.SingleBorderWindow;
        winInputDialog.ResizeMode = ResizeMode.CanResizeWithGrip;
        winInputDialog.Name = "InputBoxDialog";

我已将画布的宽度和高度属性设置为窗口。但是为什么我的屏幕是这样的:

为什么控件和窗口边框之间有空间,即使它们在视图框中。我什至尝试过 Cliptobounds,但还是一样。

如果我设置 Viewbox 的高度和宽度,它不会拉伸并且表现得不像 Viewbox。

我想动态设置这个屏幕。怎么样?

示例项目位于http://122.160.24.172/download/customer_data/InputBox_New.rar

【问题讨论】:

    标签: c# wpf user-controls wpf-controls


    【解决方案1】:

    如果您希望您的窗口具有动态布局,为什么不使用动态容器而不是 Canvas 是静态的?

    你可以像这样使用Grid -

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="0.8*"/>
            <ColumnDefinition Width ="0.2*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock Text="Hello"/>
        <Button Grid.Column="1" Content="Ok"/>
        <Button Grid.Row="1" Grid.Column="1" Content="Cancel"/>
        <TextBox Grid.Row="2" Grid.ColumnSpan="2" Text="Hello"/>
    </Grid>
    

    这样,您的窗口将在其大小更改时自行布局。 如果您愿意,您仍然可以调整按钮的大小和边距。

    不要使用Canvas,除非您确实需要支持精确的像素协调定位布局。

    另外:为什么要以编程方式而不是 XAML 来布局窗口?

    【讨论】:

    • 实际上它是一个输入框,它在我的应用程序中经常用于接受用户输入。如果它是一个 XAML 窗口,我必须每次都创建它的实例才能使用它。这会使我的代码混乱。事实上,我使用这个动态窗口的静态函数,它自己调用初始化组件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-27
    相关资源
    最近更新 更多