【问题标题】:wpf Auto Width UserControl in ListBox列表框中的wpf自动宽度用户控件
【发布时间】:2015-10-07 13:27:15
【问题描述】:

大家好,我创建了这个UserControl

<UserControl x:Class="Pass.ConexionControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid MinWidth="300">
        <Grid.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FF8D9395"/>
                <GradientStop Color="Gainsboro" Offset="1"/>
            </LinearGradientBrush>
        </Grid.Background>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"/>
            <RowDefinition/>
            <RowDefinition Height="30"/>
            <RowDefinition/>
            <RowDefinition Height="30"/>
            <RowDefinition/>
            <RowDefinition Height="30"/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="30" MinWidth="30" MaxWidth="30"/>
        </Grid.ColumnDefinitions>
        <Label Grid.Row="0" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Foreground="White"/>
        <Label Grid.Row="2" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Foreground="White"/>
        <Label Grid.Row="4" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Foreground="White"/>
        <Label Grid.Row="6" HorizontalAlignment="Left" Margin="0,0,0,0" VerticalAlignment="Top" Foreground="White"/>

        <TextBox Grid.Row="1" ></TextBox>
        <TextBox Grid.Row="3"></TextBox>
        <TextBox Grid.Row="5"></TextBox>
        <TextBox Grid.Row="7"></TextBox>

        <Button Grid.Column="1" Grid.Row="1" Click="Button_Click"></Button>
        <Button Grid.Column="1" Grid.Row="3" Click="Button_Click_1"></Button>
        <Button Grid.Column="1" Grid.Row="5" Click="Button_Click_2"></Button>
        <Button Grid.Column="1" Grid.Row="7" Click="Button_Click_3"></Button>

    </Grid>
</UserControl>

控件在 Visual Studio 设计器中正确显示

当我尝试将其添加到Window 中的ListBox 时,控制合同的宽度

我将属性 MinWidth 添加为 300,但是,这不是我需要的行为

我的MainWindow 是这样的

<Window
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Pass" x:Class="Pass.MainWindow"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <ListBox x:Name="listMain" Margin="5">
            <local:ConexionControl />
        </ListBox>

    </Grid>
</Window>

在这种情况下,当调整窗口大小时,列表控件也会改变大小。

我需要用户控件调整列表框的大小

在我的用户控件中,我有 1 个 2 列的网格,第一列必须调整大小,第二列必须保持大小。

我应该改变什么来完成这个?

【问题讨论】:

  • 将宽度设置为ListBox,然后在用户控件中将第一列宽度设置为Auto,第二列设置为*
  • 检查this

标签: c# wpf user-controls listbox


【解决方案1】:
<ListBox HorizontalContentAlignment="Stretch" Margin="0,0,164,86">
  <uc:UserControl1 />
</ListBox>

在您的用户控制中:

<Grid>
...
<Grid.ColumnDefinitions>
  <ColumnDefinition Width="*"/>
  <ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>

现在,当您调整窗口大小时,所有内容都会相应地扩展/收缩。

【讨论】:

    猜你喜欢
    • 2010-11-06
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多