ListBox 中的 wpf 自动宽度用户控件
wpf Auto Width UserControl in 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
控件宽度收缩
我用 300 添加 属性 MinWidth
,但是,这不是我需要的行为
我的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>
在这种情况下,当 window 调整大小时,列表控件的大小也会改变。
我需要调整 ListBox 的用户控件大小
在我的用户控件中,我有 1 个网格和 2 列,第一列必须调整大小,第二列必须保持大小。
我应该更改什么才能实现此目的?
<ListBox HorizontalContentAlignment="Stretch" Margin="0,0,164,86">
<uc:UserControl1 />
</ListBox>
在您的用户控件中:
<Grid>
...
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
现在,当您调整 window 大小时,一切都会 expand/contract 相应。
大家好,我创建了这个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
控件宽度收缩
我用 300 添加 属性 MinWidth
,但是,这不是我需要的行为
我的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>
在这种情况下,当 window 调整大小时,列表控件的大小也会改变。
我需要调整 ListBox 的用户控件大小
在我的用户控件中,我有 1 个网格和 2 列,第一列必须调整大小,第二列必须保持大小。
我应该更改什么才能实现此目的?
<ListBox HorizontalContentAlignment="Stretch" Margin="0,0,164,86">
<uc:UserControl1 />
</ListBox>
在您的用户控件中:
<Grid>
...
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100" />
</Grid.ColumnDefinitions>
现在,当您调整 window 大小时,一切都会 expand/contract 相应。