wpf ListBox中的自动宽度UserControl

时间:2015-10-06 17:14:08

标签: c# wpf user-controls listbox

大家好我创建了这个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设计器中正确显示

当我尝试将其添加到ListBox Window中控件合约的宽度时

我将属性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>

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

我需要用户控件调整ListBox

在我的用户控件中,我有1个Grid with 2 Columns,第一列必须重新调整,第二列必须保持大小。

我应该改变什么才能做到这一点?

1 个答案:

答案 0 :(得分:0)

<ListBox HorizontalContentAlignment="Stretch" Margin="0,0,164,86">
  <uc:UserControl1 />
</ListBox>

在您的用户控件中:

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

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