【问题标题】:How to dynamically re-size user control and contents?如何动态调整用户控件和内容的大小?
【发布时间】:2015-12-05 19:29:07
【问题描述】:

我有一个包含在窗口中的用户控件。但我注意到,当我最大化和重新调整窗口大小时,用户控件和 UI 元素没有相应调整,如 here 所示。

我所做的尝试是在 tutorial 之后将网格包装在 ViewBox 中,但内容不会相应地重新调整大小,并且看起来像 pushed together

有人知道如何解决这个调整大小的问题吗?

用户控件xaml定义示例:

<UserControl x:Class="MongoDBApp.Views.CustomerDetailsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:email_validator="clr-namespace:MongoDBApp.Validator"
             xmlns:xctk="http://schemas.xceed.com/wpf/xaml/toolkit"
             Width="800"
             Height="500">

   <Viewbox>

    <xctk:BusyIndicator IsBusy="{Binding ButtonEnabled}">


        <Grid>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="70" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="1*" />
                    <RowDefinition Height=".50*" />
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="1*" />
                    <RowDefinition Height="1*" />
                    <RowDefinition Height=".2*" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="17*" />
                    <ColumnDefinition Width="142*" />
                    <ColumnDefinition Width="29*" />
                    <ColumnDefinition Width="171*" />
                    <ColumnDefinition Width="342*" />
                    <ColumnDefinition Width="85*" />
                </Grid.ColumnDefinitions>

                <DockPanel>
                    <ToolBarTray DockPanel.Dock="Top" Orientation="Horizontal">
                        <ToolBar>
                            <Button Command="{Binding RefreshCommand}" ToolTip="Refreshes the customer records.">
                                <Image Width="30"
                                       Height="30"
                                       Source="/MongoDBApp;component/Images/refresh.png" />
                            </Button>
                        </ToolBar>
                    </ToolBarTray>
                </DockPanel>

                <DataGrid x:Name="customersgrid"
                          Grid.Row="0"
                          Grid.RowSpan="3"
                          Grid.Column="1"
                          Grid.ColumnSpan="4"
                          AutoGenerateColumns="False"
                          ItemsSource="{Binding Customers}"
                          SelectedItem="{Binding SelectedCustomer}">
                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="{Binding Id}" Header="ID" />
                        <DataGridTextColumn Binding="{Binding FirstName}" Header="First Name" />
                        <DataGridTextColumn Binding="{Binding LastName}" Header="Last Name" />
                        <DataGridTextColumn Binding="{Binding Email}" Header="Email" />
                        <DataGridTextColumn Binding="{Binding Address}" Header="Address" />
                        <DataGridTextColumn Binding="{Binding Country}" Header="Country" />
                    </DataGrid.Columns>
                </DataGrid>

                <Label Grid.Row="4"
                       Grid.Column="1"
                       Margin="51,0,20.672,0"
                       HorizontalAlignment="Center"
                       VerticalAlignment="Top"
                       Content="First Name:" />
                <TextBox x:Name="fNameTbx"
                         Grid.Row="4"
                         Grid.Column="3"
                         Width="120"
                         Height="23"
                         HorizontalAlignment="Left"
                         VerticalAlignment="Top"
                         Text="{Binding SelectedCustomer.FirstName}"
                         TextWrapping="Wrap" />

                <TextBlock x:Name="iDTbx"
                           Grid.Row="4"
                           Grid.Column="4"
                           Width="180"
                           Height="23"
                           HorizontalAlignment="Right"
                           VerticalAlignment="Top"
                           Text="{Binding SelectedCustomer.Id}"
                           TextWrapping="Wrap" />

                <Label Grid.Row="6"
                       Grid.Column="4"
                       HorizontalAlignment="Left"
                       VerticalAlignment="Top"
                       Content="Country:" />

                <ComboBox Grid.Row="6"
                          Grid.Column="4"
                          Width="180"
                          Height="30"
                          Margin="84,9,84,0"
                          HorizontalAlignment="Center"
                          VerticalAlignment="Top"
                          DisplayMemberPath="Name"
                          ItemsSource="{Binding Countries}"
                          ScrollViewer.VerticalScrollBarVisibility="Visible"
                          Text="{Binding SelectedCustomer.Country}" />



                <Button x:Name="addBtn"
                        Grid.Row="7"
                        Grid.Column="1"
                        Width="75"
                        HorizontalAlignment="Left"
                        VerticalAlignment="Top"
                        Command="{Binding AddCommand}"
                        Content="Add" />
            </Grid>
        </Grid>
    </xctk:BusyIndicator>
  </Viewbox>
</UserControl>

以及拥有 UserControl 的 Window:

<Window x:Class="MongoDBApp.Views.ApplicationView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:views="clr-namespace:MongoDBApp.Views"
        xmlns:vm="clr-namespace:MongoDBApp.ViewModels"
        Title="ApplicationView"
        Width="800"
        Height="500">

    <Window.Resources>
        <DataTemplate DataType="{x:Type vm:CustomerDetailsViewModel}">
            <views:CustomerDetailsView />
        </DataTemplate>
    </Window.Resources>

    <Window.DataContext>
        <vm:ApplicationViewModel />
    </Window.DataContext>


    <TabControl ItemsSource="{Binding PageViewModels}"
                SelectedItem="{Binding CurrentPageViewModel}"
                TabStripPlacement="Top">
        <TabControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}" />
            </DataTemplate>
        </TabControl.ItemTemplate>
        <TabControl.ItemContainerStyle>
            <Style TargetType="{x:Type TabItem}">
                <Setter Property="IsEnabled" Value="{Binding IsEnabled}" />
            </Style>
        </TabControl.ItemContainerStyle>
    </TabControl>
</Window>

【问题讨论】:

    标签: wpf xaml user-controls window


    【解决方案1】:

    首先,将HeightRowDefinition 设置为大数字是不正确的,因为您想要调整大小的窗口,因此您需要按比例声明HeightWidth。没有像Height="70*"Width=342*这样的比例。

    <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="2*" />
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
                <RowDefinition Height="1*" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="1*" />
                <ColumnDefinition Width="4*" />
                <ColumnDefinition Width="2*" />
                <ColumnDefinition Width="1*" />
                <ColumnDefinition Width="2*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
    
            <DockPanel>
                <ToolBarTray DockPanel.Dock="Top" Orientation="Horizontal">
                    <ToolBar>
                        <Button Command="{Binding RefreshCommand}" ToolTip="Refreshes the customer records.">
                            <Image Width="30"
                                       Height="30"
                                       />
                        </Button>
                    </ToolBar>
                </ToolBarTray>
            </DockPanel>
    
            <DataGrid x:Name="customersgrid"
                          Grid.Row="0"
                          Grid.RowSpan="3"
                          Grid.Column="1"
                          Grid.ColumnSpan="4"
                          AutoGenerateColumns="False"
                          ItemsSource="{Binding Customers}"
                          SelectedItem="{Binding SelectedCustomer}">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Id}" Header="ID" />
                    <DataGridTextColumn Binding="{Binding FirstName}" Header="First Name" />
                    <DataGridTextColumn Binding="{Binding LastName}" Header="Last Name" />
                    <DataGridTextColumn Binding="{Binding Email}" Header="Email" />
                    <DataGridTextColumn Binding="{Binding Address}" Header="Address" />
                    <DataGridTextColumn Binding="{Binding Country}" Header="Country" />
                </DataGrid.Columns>
            </DataGrid>
    
            <Label Grid.Row="4"
                       Grid.Column="1"                       
                       HorizontalAlignment="Center"
                       VerticalAlignment="Top"
                       Content="First Name:" />
            <TextBox x:Name="fNameTbx"
                         Grid.Row="4"
                         Grid.Column="3"
                         MinWidth="20"
                         HorizontalAlignment="Left"
                         VerticalAlignment="Top"
                         Text="{Binding SelectedCustomer.FirstName}"
                         TextWrapping="Wrap" />
    
            <TextBlock x:Name="iDTbx"
                           Grid.Row="4"
                           Grid.Column="4"                           
                           HorizontalAlignment="Right"
                           VerticalAlignment="Top"
                           Text="{Binding SelectedCustomer.Id}"
                           TextWrapping="Wrap" />
    
            <Label Grid.Row="6"
                       Grid.Column="4"
                       HorizontalAlignment="Left"
                       VerticalAlignment="Top"
                       Content="Country:" />
    
            <ComboBox Grid.Row="6"       Grid.Column="4"
    
    
                          HorizontalAlignment="Center"
                          VerticalAlignment="Top"
                          DisplayMemberPath="Name"
                          ItemsSource="{Binding Countries}"
                          ScrollViewer.VerticalScrollBarVisibility="Visible"
                          Text="{Binding SelectedCustomer.Country}" />
    
    
    
            <Button x:Name="addBtn"
                        Grid.Row="7"
                        Grid.Column="1"                        
                        HorizontalAlignment="Left"
    
                        Command="{Binding AddCommand}"
                        Content="Add" />
        </Grid>
    

    然后,如果您希望 UserControl 可调整大小,则无需为您的 UserControl 设置大小。所以删除这个设置:

    Width="800"
    Height="500"
    

    Resizable Window 的好例子:

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="28" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="200" />
        </Grid.ColumnDefinitions>
        <Label Grid.Row="0" Grid.Column="0" Content="Name:"/>
        <Label Grid.Row="1" Grid.Column="0" Content="E-Mail:"/>
        <Label Grid.Row="2" Grid.Column="0" Content="Comment:"/>
        <TextBox Grid.Column="1" Grid.Row="0" Margin="3" />
        <TextBox Grid.Column="1" Grid.Row="1" Margin="3" />
        <TextBox Grid.Column="1" Grid.Row="2" Margin="3" />
        <Button Grid.Column="1" Grid.Row="3" HorizontalAlignment="Right" 
                MinWidth="80" Margin="3" Content="Send"  />
    </Grid>
    

    看到这个tutorial

    【讨论】:

      【解决方案2】:

      您已指定:

      Width="800"
      Height="500"
      

      在用户控件中。也许是这种行为的原因。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-02
        • 2014-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-31
        相关资源
        最近更新 更多