簡體   English   中英

WPF,屬性不返回綁定值

[英]WPF, Property does not return value to the binding

因此,我有一個帶有滾動文本(marqee)的項目,該項目在字符串數組上旋轉。 我希望它在每次動畫迭代20秒后更改字符串值。

但是,存在一個問題,使用INotifyPropertyChanged接口綁定到文本塊(使用XAML)的屬性(ScrollingText)在第一次迭代后不會返回。 即使它正常刷新(在set部分中),它也不會在Getter部分返回。

主要類別:

class GetScrollingText : CommonBase
{
    private string _scrollingtext = String.Empty;
    DoubleAnimation Animation;

    public GetScrollingText()
    {
        ScrollingText = GetScrollString();
    }

    public string ScrollingText
    {
        get
        {
            return _scrollingtext;
        }
        set
        {
            if (value != _scrollingtext)
            {
                _scrollingtext = value;
                RaisePropertyChanged("ScrollingText");
            }               
        }
    } // INJECTS the string in the animated textblock {binding}.

    public TextBlock scrollBlock { get; set; }

    string GetScrollString()
    {
        .........
        return scrolltext;
    }      

    public void LeftToRightMarqee(double from, double to)
    {
        Animation = new DoubleAnimation();
        Animation.From = from;
        Animation.To = to;
        Animation.Duration = new Duration(TimeSpan.FromSeconds(20));
        Animation.Completed += animation_Completed;
        scrollBlock.BeginAnimation(Canvas.LeftProperty, Animation);
    }

    void animation_Completed(object sender, EventArgs e)
    {
        ScrollingText = GetScrollString();           
        scrollBlock.BeginAnimation(Canvas.LeftProperty, Animation);
    }
}

由於某種原因,animation_Completed事件僅更改值ScrollingText,但不調用Getter部分,因此不會返回到{binding}。

XAML:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:vm="clr-namespace:AnnouncingSys"
    x:Class="AnnouncingSys.MainWindow"
    x:Name="Window"
    Width="1280" Height="720" MinHeight="566" MinWidth="710">

    <Window.Resources>
        <vm:GetScrollingText x:Key="ScrollingText"/>
    </Window.Resources>
    <Canvas x:Name="MainCanvas" ClipToBounds="True" Margin="0,0,0,0" Grid.Row="5" Background="Black" Grid.ColumnSpan="5" >
        <TextBlock x:Name="ScrollBlock" TextWrapping="Wrap" VerticalAlignment="Top" Height="113" Width="5147" Canvas.Left="-1922" Text="{Binding ScrollingText, Source={StaticResource ScrollingText}}"/>
    </Canvas>
</Window>

代碼如下:

public partial class MainWindow : Window
{
    GetScrollingText scrolling = new GetScrollingText();

    public MainWindow()
    {
        InitializeComponent();
        scrolling.scrollBlock = this.ScrollBlock;
        scrolling.LeftToRightMarqee(2000, -3000);
    }
}

最后是輔助類CommonBase:

public class CommonBase : INotifyPropertyChanged
{
    protected CommonBase()
    {
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string PropertyName)
    {
        PropertyChangedEventHandler handler = this.PropertyChanged;

        if (handler != null)
        {
            PropertyChangedEventArgs e = new PropertyChangedEventArgs(PropertyName);
            handler(this, e);
        }
    }
}

我什至在Getter的返回塊上放置了一個斷點,但僅在第一個斷點上激活:“ ScrollingText = GetScrollString()”。 我的意思是,每次更改值后都不應該返回嗎???

您正在使用GetScrollingText類的兩個不同實例,一個實例在XAML中作為StaticResource,另一個在后面的代碼中作為類MainWindow中的scrolling字段。

除了在XAML中創建StaticResource之外,您還可以設置MainWindow的DataContext屬性:

public partial class MainWindow : Window
{
    GetScrollingText scrolling = new GetScrollingText();

    public MainWindow()
    {
        InitializeComponent();
        scrolling.scrollBlock = this.ScrollBlock;
        scrolling.LeftToRightMarqee(2000, -3000);
        DataContext = scrolling; // here
    }
}

現在,您無需顯式設置綁定的Source屬性,因為DataContext用作默認綁定源:

<TextBlock ... Text="{Binding ScrollingText}"/>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM