【问题标题】:How to programically change ScaleY RenderTransform of a control in a XAML C# Windows App?如何以编程方式更改 XAML C# Windows 应用程序中控件的 ScalY RenderTransform?
【发布时间】:2015-08-26 01:33:27
【问题描述】:

我有以下 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() 语句之前都会更改。我的代码如下。其中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();
}

代码的折叠部分在后面的else中非常相似。 Stop() 命令是必要的,因为如果它们不在那里,我会收到一个错误,说在重新定位之前必须停止根情节提要。所以我所说的一切都很好。不起作用的是,如果我扩展第一个网格,因此 arrowSection_Lec_1 的 ScaleY 为 -1,如果我随后扩展第二个网格,将 arrowSection_Lec_2 的 ScaleY 也设为 -1,则第一个图像恢复为 ScaleY 1 即使其对应的网格仍在扩展。

我想到的解决方案是让情节提要的 Completed 事件明确设置适当箭头的 ScaleY,以便即使情节提要再次针对不同的箭头运行,它也能保持此位置。我不知道如何在 C# 中引用这个属性。

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

【问题讨论】:

    标签: c# xaml animation visual-studio-2015


    【解决方案1】:

    下面是如何从代码隐藏中访问 Scale:

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

    【讨论】:

    • 谢谢!像魅力一样工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    • 2016-09-05
    • 2016-08-08
    相关资源
    最近更新 更多