如果我在故事板上设置源代码,为什么图像是空的?

时间:2013-12-23 09:05:53

标签: wpf silverlight windows-phone-8

我正在编写自定义WP8控件,我需要从VisualStateManager管理的故事板中更改图像源。

我的XAML

<Style TargetType="local:StateImageButton">

    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="ImageStretch" Value="Uniform" />

    <Setter Property="InactiveImage" Value="/StarLine.Controls;component/Resources/StateImageButton/ButtonInactive.png" />
    <Setter Property="DisabledImage" Value="/StarLine.Controls;component/Resources/StateImageButton/ButtonDisabled.png" />

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:StateImageButton">
                <Grid>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal" >
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="viewImage" Storyboard.TargetProperty="Source">
                                        <DiscreteObjectKeyFrame KeyTime="0:0" Value="{TemplateBinding InactiveImage}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="viewImage" Storyboard.TargetProperty="Source">
                                        <DiscreteObjectKeyFrame KeyTime="0:0" Value="{TemplateBinding DisabledImage}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>

                    </VisualStateManager.VisualStateGroups>

                    <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                        <Image x:Name="viewImage"
                               Stretch="{TemplateBinding ImageStretch}"
                               Margin="{TemplateBinding Padding}"
                               HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                               VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是一个依赖属性声明

public static readonly DependencyProperty InactiveImageProperty =
            DependencyProperty.Register("InactiveImage", typeof(ImageSource), typeof(StateImageButton), null);

        [TypeConverter(typeof(ImageSourceConverter))]
        public ImageSource InactiveImage
        {
            get { return (ImageSource)GetValue(InactiveImageProperty); }
            set { SetValue(InactiveImageProperty, value); }
        }

最后是一个改变VisualState的代码

private void UpdateState()
        {
            if(IsEnabled)
            {
                VisualStateManager.GoToState(this, "Normal", false);
            }
            else
            {
                VisualStateManager.GoToState(this, "Disabled", false);
            }           
        }

所有部件看起来都是正确的,但图像源不会改变

0 个答案:

没有答案