简体   繁体   English

在本地窗口资源中使用时,为什么绑定到属性不起作用?

[英]Why doesn't binding to a property work when used in local window resources?

I have a storyboard with an animation in MainWindow.xaml Window.Resources section: 我在MainWindow.xaml Window.Resources部分中有一个带有动画的情节提要:

<Window.Resources>
    <Storyboard x:Key="ShowSearchGrid">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Height)" Storyboard.TargetName="SearchGrid">
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="{Binding ExpandedGridHeight}"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
</Window.Resources>

The property ExpandedGridHeight is defined in a custom class like so: 属性ExpandedGridHeight在自定义类中定义,如下所示:

class ExpandedGridHeightClass : INotifyPropertyChanged
{
    //The next bit handles the INotifyPropertyChanged implementation

    public event PropertyChangedEventHandler PropertyChanged;

    protected void Notify(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public Double ExpandedGridHeight
    {
        get { return _expandedGridHeight; }
        set
        {
            if (value != _expandedGridHeight)
            {
                _expandedGridHeight = value;
                Notify("ExpandedGridHeight");
            }
        }
    }

    private Double _expandedGridHeight = 100;
}

The data context for the window is set in MainWindow(): 在MainWindow()中设置窗口的数据上下文:

ExpandedGridHeightClass ExpandedHeight;

public MainWindow()
{
    InitializeComponent();

    ExpandedHeight = new ExpandedGridHeightClass();
    this.DataContext = ExpandedHeight;
}

The actual value for ExpandedHeight's ExpandedGridHeight property is set elsewhere, and changes depending on the size of the window. ExpandedHeight的ExpandedGridHeight属性的实际值在其他位置设置,并根据窗口的大小而变化。

The storyboard is started from the following function: 故事板从以下功能启动:

void showSearchGrid()
{
    Storyboard ShowSearchGrid = this.FindResource("ShowSearchGrid") as Storyboard;
    ShowSearchGrid.Begin();
}

When I tried to bind the property to various other places like to the title of the window, it worked perfectly, and updated exactly as intended. 当我尝试将属性绑定到其他位置(例如窗口的标题)时,它可以正常工作,并且完全按照预期进行了更新。 However, when it is binded like in the code examples to a property in the local resources, it doesn't work: the animation does start as usual, but instead of animating to ExpandedGridHeight, the SearchGrid is animated to a height value of 0. I'm suspecting it is because the animation is defined in the resources, but don't know a fix to this problem. 但是,当像代码示例中那样将其绑定到本地资源中的属性时,它不起作用:动画确实照常开始,但不是将动画设置为ExpandedGridHeight,而是将SearchGrid动画为高度值0。我怀疑这是因为动画是在资源中定义的,但不知道解决此问题的方法。 Any help would be appreciated! 任何帮助,将不胜感激! :) :)

Just thought I might as well put this as an answer. 只是以为我最好把它当作答案。 This method is definitely "hackish", so it may not be to your liking. 此方法绝对是“ hackish”,因此可能不符合您的喜好。

ExpandedGridHeightClass: ExpandedGridHeightClass:

class ExpandedGridHeightClass : INotifyPropertyChanged
{
    private static ExpandedGridHeightClass _instance;
    public static ExpandedGridHeightClass Instance
    {
        get
        {
            if (_instance == null)
                _instance = new ExpandedGridHeightClass();
            return _instance;
        }
    }

    /**
     * Whatever your class already has
     */
}

XAML: XAML:

<EasingDoubleKeyFrame KeyTime="0:0:0.5"
    Value="{Binding ExpandedGridHeight, Source={x:Static MyNamespace:ExpandedGridHeightClass.Instance}}"/>

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

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