我们需要在UWP应用程序中为同一.cs创建4个不同的.xaml布局。这些布局的结构取决于db的值“ questionType”。
每个布局的变体应包含相同的控件,但位置不同(即,每个变体应包含一个图像,一个richTextEditor和由4个单选组成的radioGroup)
例如,如果questionType = 1,则图像应放置在屏幕的左侧,如果questionType = 2,则图像应放置在RTF编辑器的顶部,收音机也应水平放置... < / p>
我们已经考虑并尝试过的事情:
到目前为止,我们已经尝试了视觉状态管理器,但是不幸的是,使用它我们只能更改对齐方式,而不能更改控件的位置。
我们还检查了条件xaml,但看来它仅用于版本适应性。
具有不断变化的可见性的面板。但是我们决定不采用此解决方案,因为它非常难看。
任何将我们引向正确方向的人,将不胜感激。
谢谢。
编辑:
<VisualState x:Name="Layout1">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.HorizontalAlignment)"
Storyboard.TargetName="ContentRoot">
...
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
答案 0 :(得分:1)
VisualStateManager可以更改您在元素上定义的任何属性,而不仅仅是Alignments。
为简单起见,我使用两个Border
来表示Image
和RichTextBox
。最初,图像位于左侧,然后使用VisualStateManager
转到另一种视觉状态,其中Image
位于顶部。请注意,属性(Grid.Column)
和(Grid.Row)
的更改就像(FrameworkElement.HorizontalAlignment)
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Border x:Name="imageControl"
Background="Red"
Height="200" Width="200"
Grid.Row="1" />
<Border x:Name="richTextBoxControl"
Background="Green"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Grid.Row="1" Grid.Column="1" />
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="Layout1" />
<VisualState x:Name="Layout2">
<Storyboard>
<ObjectAnimationUsingKeyFrames
Storyboard.TargetName="imageControl"
Storyboard.TargetProperty="(Grid.Column)">
<DiscreteObjectKeyFrame KeyTime="0" Value="1" />
</ObjectAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames
Storyboard.TargetName="imageControl"
Storyboard.TargetProperty="(Grid.Row)">
<DiscreteObjectKeyFrame KeyTime="0" Value="0" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
然后在后面的代码中,根据值VisualState
更改questionType
。
if (questionType == 1)
return; //Layout1 is the default state
else if (questionType == 2)
VisualStateManager.GoToState(this, "Layout2", true);
还有其他方法,例如使用StackPanel承载控件,这些控件最初为水平方向,然后在其他可视状态下将其更改为垂直方向。