【问题标题】:Storyboard in DataTemplate doesn't workDataTemplate 中的情节提要不起作用
【发布时间】:2012-03-17 05:56:24
【问题描述】:

我需要有一个故事板,它可以改变我的 TextBox 中的前景。问题是这个 TextBox 必须在 DataTemplate 中。

如何更改我的 xaml 以使其正常工作?

 <DataTemplate x:Key="contentTexBox">
            <Grid>
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="CommonStates">
                        <VisualState x:Name="Normal"/>
                        <VisualState x:Name="MouseOver">
                            <Storyboard>
                                <ColorAnimation Duration="0" To="Pink" Storyboard.TargetProperty="(Control.Foreground).(SolidColorBrush.Color)" Storyboard.TargetName="tbContent"/>
                            </Storyboard>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
                <TextBox Text="Test text" Width="200" Height="35" Foreground="Blue" x:Name="tbContent" BorderBrush="Purple">                   

                </TextBox>
            </Grid>
        </DataTemplate>

【问题讨论】:

  • 对不起,我想我没听懂这个问题,因为您的示例有效。有什么问题?
  • 鼠标悬停时前景是否从粉色变为蓝色?

标签: silverlight storyboard datatemplate


【解决方案1】:

我用另一种方式做了,但这个方法没有明确引用文本框,所以我想它应该适用于你的情况。主要变化是对样式 TextBox 的调用现在是隐式的(因为状态是在此控件下声明的)并且 Background 属性被更改而不是 Foreground

根据您的情况调整我编写的测试代码后,我想它看起来或多或少是这样的:

<DataTemplate x:Key="contentCheckBox">
<Grid>
<TextBox Text="Test text" Width="200" Height="35" Foreground="Blue" BorderBrush="Purple">
        <TextBox.Style>
            <Style TargetType="TextBox">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="TextBox">
                            <TextBox Width="190">
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="CommonStates">
                                        <VisualState x:Name="Normal"/>
                                        <VisualState x:Name="MouseOver">
                                            <Storyboard>
                                                <ColorAnimation Duration="0" To="Pink" Storyboard.TargetProperty="Background.Color" />
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                            </TextBox>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </TextBox.Style>
    </TextBox>
    </Grid>
    </DataTemplate>

【讨论】:

  • 感谢@Lucas 的帮助。我必须在这里设置一个 Target/TargetName,但我不知道怎么做。你能编辑你的答案吗?
  • 和 TargetType="{x:Type TextBox}" 应该是 TargetType="TextBox" 。 x:类型在 wpf 中
  • 是的,抱歉,我已经在 WPF 中使用 x:Type 对此进行了测试。在谈到TargetName 时,您是否尝试过不设置它(如sn-p 中所写)? Storyboard 被定义为TextBox 的子元素,所以我想你不必明确指定它。
猜你喜欢
  • 1970-01-01
  • 2016-05-02
  • 1970-01-01
  • 2015-08-27
  • 1970-01-01
  • 2018-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多