繁体   English   中英

如何在XAML C#Windows App中以编程方式更改控件的ScaleY RenderTransform?

[英]How to programically change ScaleY RenderTransform of a control in a XAML C# Windows App?

我有以下xaml代码的多个实例。 从本质上讲,它是一堆网格,它们以小的状态(24像素高)显示在堆栈面板中,显示一行文本块。 当您单击箭头图像(或图像周围的边框,因为图像中具有透明性)时,网格将展开以显示其中的所有细节。 我总共有15个:

<Grid x:Name="borLecSec1" Style="{StaticResource SearchedSectionGrid}">
    <TextBlock x:Name="txtLecSec1" Style="{StaticResource SearchedSectionText}" 
               PointerEntered="SearchSectionEntered" PointerExited="SearchSectionExited" 
               Tapped="SearchSectionTapped"/>

    <Border x:Name="backArrowSection_Lec_1" Style="{StaticResource ExpandSectionButton}" 
            PointerEntered="backArrowSectionEnter" PointerExited="backArrowSectionExit" 
            Tapped="backArrowSectionTapped">

        <Image x:Name="arrowSection_Lec_1" Style="{StaticResource ExpandSectionImage}">
            <Image.RenderTransform>
                <CompositeTransform ScaleY="1"/>
            </Image.RenderTransform>
        </Image>

    </Border>
</Grid>

我还没有弄清楚如何对自己扩展的网格进行动画处理,因为它们必须从24像素变为“自动”,而我还没有工作。 我要做的是单击该箭头时,该箭头垂直翻转,因此它表示再次单击将导致网格折叠。 该动画的情节提要是这样的:

<Storyboard x:Name="SearchSectionArrowExpand">
    <DoubleAnimation  Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleY)" 
                      From="1" To="-1" Duration="00:00:0.15"/>
</Storyboard>

<Storyboard x:Name="SearchSectionArrowCollapse">
    <DoubleAnimation  Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleY)" 
                      From="-1" To="1" Duration="00:00:0.15"/>
</Storyboard>

由于此箭头图像控件有很多实例,因此,在后面的C#代码中的每个Begin()语句之前,都会更改情节提要的target属性。 我的代码如下。 在其中,working_grid和working_image对象分别对应于上述xaml中的borLecSec1和arrowSection_Lec_1。

if (working_grid.Height == 24)
{
    System.Diagnostics.Debug.WriteLine("expand");
    working_grid.Height = double.NaN;

    SearchSectionArrowCollapse.Stop();
    SearchSectionArrowExpand.Stop();

    Storyboard.SetTargetName(SearchSectionArrowExpand, working_image_name);
    SearchSectionArrowExpand.Begin();
}

该代码的折叠部分在随后的其他部分中非常相似。 Stop()命令是必需的,因为如果它们不存在,则说我必须先停止根情节提要,然后再重新定向,否则我会收到错误消息。 所以我所说的一切都很好。 不起作用的是,如果我扩展了第一个网格,则arrowSection_Lec_1的ScaleY为-1,如果我然后扩展了第二个网格,并且给arrowSection_Lec_2的ScaleY也为-1,则第一张图像将还原为ScaleY 1,即使其对应的网格仍在扩展。

我想到的解决方案是,使故事板“完成”事件显式设置适当箭头的ScaleY,这样即使故事板针对另一个箭头再次运行,它也将保持此位置。 我不知道如何在C#中引用此属性。

因此,为清楚起见,我的问题是如何从后面的代码中将arrowSection_Lec_1的ScaleY转换设置为-1?

这是从后面的代码访问Scale的方法:

var transform = (CompositeTransform)arrowSection_Lec_1.RenderTransform;
transform.ScaleY = -1;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM