[UWP]大家好,我正在努力与一个用户控件,我创建我自己的字段容器(groupbox)里面我发布不同的StackPanel,在文本框内。用户控制内容控制不填充
但是当我插入StackPanel时,你不能有效地填充空白空间。我试图在各种选项中设置strecth,但没有。
这是我的用户的代码:
<UserControl Name="groupBoxUC"
x:Class="AgendaUWP.Common.CustomControls.GroupBox"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:AgendaUWP.Common.CustomControls"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:interop="using:Windows.UI.Xaml.Interop"
mc:Ignorable="d"
d:DesignHeight="200"
d:DesignWidth="300">
<StackPanel x:Name="myStackPanel" BorderThickness="1" BorderBrush="Gray" Orientation="Vertical">
<StackPanel Margin="10 0 0 0" HorizontalAlignment="Left" >
<Border Background="{StaticResource secondaryColor}" Height="5" Width="{Binding ActualWidth, ElementName=HeaderText}"/>
<TextBlock x:Name="HeaderText" FontSize="18" FontWeight="ExtraLight" Text="{x:Bind Header,Mode=TwoWay}"/>
</StackPanel>
<ContentControl Padding="10 20 10 20" Content="{Binding InnerContent,ElementName=groupBoxUC}" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch" />
</StackPanel>
我的代码Bheind用户控件:
[ContentProperty(Name = "InnerContent")]
public sealed partial class GroupBox : UserControl
{
internal static readonly DependencyProperty InnerContentProperty =
DependencyProperty.Register("InnerContent", typeof(object), typeof(GroupBox), new PropertyMetadata(null));
internal static readonly DependencyProperty HeaderProperty =
DependencyProperty.Register("Header", typeof(string), typeof(GroupBox), new PropertyMetadata(null));
public GroupBox()
{
this.InitializeComponent();
}
public object InnerContent
{
get { return (object)GetValue(InnerContentProperty); }
set { SetValue(InnerContentProperty, value); }
}
public string Header
{
get { return (string) GetValue(HeaderProperty); }
set { SetValue(HeaderProperty, value); }
}
}
这是我如何使用它:
<CustomControls:GroupBox x:Name="grpCoordinate" Header="Coordinate" Margin="30,40,30,0" >
<StackPanel HorizontalAlignment="Left">
<TextBox Header="Longitudine:" Text="{x:Bind ViewModel.Cliente.Longitudine, Mode=TwoWay}" HorizontalAlignment="Stretch" />
<TextBox Header="Latitudine:" Text="{x:Bind ViewModel.Cliente.Latitudine, Mode=TwoWay}" HorizontalAlignment="Stretch" />
</StackPanel>
</CustomControls:GroupBox>
这是我的结果:
编辑:
原谅我,但我错了粘贴代码和图像。我纠正: 我的用法如下:
<CustomControls:GroupBox x:Name="grpDatiAnagrafici" Header="Dati Anagrafici" VerticalAlignment="Top" Margin="30,0,30,0" Grid.Column="0" Grid.Row="0" >
<StackPanel x:Name="stkDatiAnagrafici" Orientation="Horizontal">
<StackPanel Padding="0 0 20 0">
<TextBox Header="Codice:" Text="{x:Bind ViewModel.Cliente.Codice, Mode=TwoWay}" IsReadOnly="True" IsEnabled="False"/>
<TextBox Header="Ragione Sociale:" Text="{x:Bind ViewModel.Cliente.RagioneSociale, Mode=TwoWay}" />
<TextBox Header="Nome:" Text="{x:Bind ViewModel.Cliente.Nome, Mode=TwoWay}" />
<TextBox Header="Cognome:" Text="{x:Bind ViewModel.Cliente.Cognome, Mode=TwoWay}" />
</StackPanel>
<StackPanel>
<TextBox Header="Indirizzo:" Text="{x:Bind ViewModel.Cliente.Indirizzo, Mode=TwoWay}" />
<TextBox Header="Comune:" Text="{x:Bind ViewModel.Cliente.Comune, Mode=TwoWay}" />
<TextBox Header="Cap:" Text="{x:Bind ViewModel.Cliente.Cap, Mode=TwoWay}" />
<TextBox Header="Provincia:" Text="{x:Bind ViewModel.Cliente.Provincia, Mode=TwoWay}" />
</StackPanel>
</StackPanel>
</CustomControls:GroupBox>
编辑2:
为澄清感谢,但有一种方法来保持的StackPanel? 我会解释,因为我要管理VisualStateManager:
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="WindowStates">
<VisualState x:Name="WideState">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="600" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="stkDatiAnagrafici.Orientation" Value="Horizontal" />
</VisualState.Setters>
</VisualState>
<VisualState x:Name="NarrowState">
<VisualState.StateTriggers>
<AdaptiveTrigger MinWindowWidth="0" />
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="stkDatiAnagrafici.Orientation" Value="Vertical" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
,因为如果我用的是网格,我还必须指定Grid.Row在设备上运行时将二次探底组。
感谢您的回复,但我只是纠正了这个问题,我错了粘贴示例 – Brux88
感谢澄清,但有一种方法来保持StackPanel? 我会解释,因为我必须管理VisualStateManager。 我编辑我的问 – Brux88